繁体   English   中英

Firebase脱机后持续写入失败

[英]Firebase persisted write failing after being offline

我有一个使用Firebase作为后端的Android应用程序。 既然Firebase支持本地持久性,我决定放弃自己的本地存储解决方案,而使用内置的解决方案。

除了一件事,一切都在工作。 离线时进行更改,关闭应用程序,然后连接并启动应用程序。

我用来更新服务器上数据的代码如下所示:

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是一些Firebase对象,位于保存此数据的根级别上。 所有这些都在与我的活动/片段连接的Service中运行

旁注:我从其他人供我使用的某些REST API中获取身份验证令牌mToken

连接到网络后,连接应用程序并进行更改:一切正常

当我没有连接时,打开应用程序,进行更改,关闭应用程序,打开应用程序并连接:一切正常

当我没有连接时,打开应用程序,进行更改,关闭应用程序,连接并打开应用程序:记录以下错误:

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

我在Firebase的文档中进行了搜索,无法确定问题出在哪里。 我想说这与身份验证有关,但是我不知道在哪里看。

所以我的问题是:这是什么问题? 我究竟做错了什么?

编辑:

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());
    }
}

它可以创建主要的Firebase实例,并以特定格式返回时间戳。 Constants.FIREBASE_URL只是https://example.firebaseio.com/

EDIT2:

mMainFirebaseInstance = FireBaseHelper.getItemsBase("1234");

可以被替换

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

可能的时间戳是:

2015-06-22 23:12:24

从DataEventListener中提供给我的快照中检索saveDataToServer中使用的ID。 例如:

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:

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

原来这是Firebase Android SDK中的错误。 在刚刚发布的2.3.1版本中已修复该问题。 参见https://www.firebase.com/docs/android/changelog.html

我可以使用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);
}

复制上述代码的步骤:

  1. 进入飞行模式
  2. 启动应用程序(以使其调用updateChildren
  3. 杀死应用
  4. 上网
  5. 启动应用

在我的测试中,第二个更新确实写入了Firebase。 但是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