简体   繁体   English

Firebase脱机后持续写入失败

[英]Firebase persisted write failing after being offline

I have an Android application that uses Firebase as backend. 我有一个使用Firebase作为后端的Android应用程序。 Now that Firebase has support for local persistence I decided to get rid of my own local storage solution and use the built-in instead. 既然Firebase支持本地持久性,我决定放弃自己的本地存储解决方案,而使用内置的解决方案。

Everything is working, except for one thing. 除了一件事,一切都在工作。 Making changes while being offline, closing the app, connect, and start the app. 离线时进行更改,关闭应用程序,然后连接并启动应用程序。

The code that I use to update my data on the server is something like this: 我用来更新服务器上数据的代码如下所示:

public void saveDataToServer(String id, Boolean isHandled) {
    Firebase firebase = new Firebase("https://example.firebaseio.com/item_data/items/1234/data").child(id);
    if(firebase == null)
        return;

    firebase.authWithCustomToken(mToken, mAuthResultHandler);

    Map<String, Object> children = new HashMap<>();
    children.put("updated_at", FireBaseHelper.getFormattedTimestamp());
    children.put("is_handled", isHandled);
    firebase.updateChildren(children);
}

mMainFirebaseInstance is some Firebase object that is on the root level of where this data is saved. mMainFirebaseInstance是一些Firebase对象,位于保存此数据的根级别上。 And this all runs in a Service that is connected to my Activities/Fragments 所有这些都在与我的活动/片段连接的Service中运行

Sidenote: I get the authentication token mToken from some REST API that someone else made for me to use. 旁注:我从其他人供我使用的某些REST API中获取身份验证令牌mToken

When I am connected, have the app connected and make changes: everything works 连接到网络后,连接应用程序并进行更改:一切正常

When I am not connected, open the app, make changes, close the app, open the app and connect: everything works 当我没有连接时,打开应用程序,进行更改,关闭应用程序,打开应用程序并连接:一切正常

When I am not connected, open the app, make changes, close the app, connect and open the app: The following error is logged: 当我没有连接时,打开应用程序,进行更改,关闭应用程序,连接并打开应用程序:记录以下错误:

06-22 17:51:52.343 28073-28395/? W/RepoOperation﹕ Persisted write at /item_data/items/7454/data/7454141033945571998119 failed: FirebaseError: Invalid token in path

I've searched in Firebase's documentation and can't figure out where the problem is. 我在Firebase的文档中进行了搜索,无法确定问题出在哪里。 I would say that this has something to do with the authentication, but I don't know anymore where to look. 我想说这与身份验证有关,但是我不知道在哪里看。

So my question is: What is the problem here? 所以我的问题是:这是什么问题? What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

The FireBaseHelper looks like this: FireBaseHelper看起来像这样:

class FireBaseHelper {
    public static Firebase getItemsBase(String itemId) {
        Firebase fb = new Firebase(Constants.FIREBASE_URL + "item_data/items/" + itemId + "/data");
        return fb;
    }

    public static String getFormattedTimestamp() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        return simpleDateFormat.format(Calendar.getInstance().getTime());
    }
}

It can create the main Firebase instance and return a timestamp in a specific format. 它可以创建主要的Firebase实例,并以特定格式返回时间戳。 Constants.FIREBASE_URL is just https://example.firebaseio.com/ Constants.FIREBASE_URL只是https://example.firebaseio.com/

EDIT2: EDIT2:

mMainFirebaseInstance = FireBaseHelper.getItemsBase("1234");

which would be replaceable by 可以被替换

mMainFirebaseInstance = new Firebase("https://example.firebaseio.com/item_data/items/1234/data");

A possible timestamp is: 可能的时间戳是:

2015-06-22 23:12:24

The id that is used in the saveDataToServer is retrieved from a snapshot that is given to me in a ValueEventListener. 从DataEventListener中提供给我的快照中检索saveDataToServer中使用的ID。 For example: 例如:

ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            HashMap data = dataSnapshot.getValue(HashMap.class);
            Set keySet = data.keySet();
            String id = keySet.get(0);
        }
        ...
}

EDIT3: EDIT3:

Android: 5.0 的Android:5.0
Firebase: 2.3.0+ Firebase:2.3.0+
Java: 7 爪哇:7

This turned out to be a bug in the Firebase Android SDK. 原来这是Firebase Android SDK中的错误。 It has been fixed in version 2.3.1 that was just released. 在刚刚发布的2.3.1版本中已修复该问题。 See https://www.firebase.com/docs/android/changelog.html 参见https://www.firebase.com/docs/android/changelog.html

I can reproduce the behavior you're seeing with this Activity.onCreate : 我可以使用Activity.onCreate复制您看到的行为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Firebase.setAndroidContext(this);
    Firebase.getDefaultConfig().setPersistenceEnabled(true);
    Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);
    Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/30987733");
    Map<String, Object> children = new HashMap<>();
    children.put("updated_at", new Date().toString());
    children.put("is_handled", true);
    ref.updateChildren(children);
}

The steps to reproduce that go with the above code: 复制上述代码的步骤:

  1. Go into airplane mode 进入飞行模式
  2. Start the app (so that it calls updateChildren 启动应用程序(以使其调用updateChildren
  3. Kill the app 杀死应用
  4. Go online 上网
  5. Start the app 启动应用

In my tests, the second update does get written to Firebase. 在我的测试中,第二个更新确实写入了Firebase。 But there is a warning written in the Android logs: 但是Android日志中有一条警告:

W/RepoOperation﹕ Persisted write at /30987733 failed: FirebaseError: Invalid token in path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM