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 对象,它位于保存此数据的根级别。这一切都在连接到我的 Activities/Fragments
的 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 实例和 return 特定格式的时间戳。 Constants.FIREBASE_URL 只是 https://example.firebaseio.com/
编辑 2:
mMainFirebaseInstance = FireBaseHelper.getItemsBase("1234");
可由
替换
mMainFirebaseInstance = new Firebase("https://example.firebaseio.com/item_data/items/1234/data");
可能的时间戳是:
2015-06-22 23:12:24
saveDataToServer 中使用的 id 是从 ValueEventListener 中提供给我的快照中检索的。例如:
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);
}
...
}
编辑 3:
Android: 5.0
火力地堡:2.3.0+
Java: 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://Whosebug.firebaseio.com/30987733");
Map<String, Object> children = new HashMap<>();
children.put("updated_at", new Date().toString());
children.put("is_handled", true);
ref.updateChildren(children);
}
重现上述代码的步骤:
- 进入飞行模式
- 启动应用程序(以便它调用
updateChildren
- 终止应用程序
- 上网
- 启动应用程序
在我的测试中,第二个更新 确实 写入了 Firebase。但是 Android 日志中写了一条警告:
W/RepoOperation﹕ Persisted write at /30987733 failed: FirebaseError: Invalid token in path
我有一个使用 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 对象,它位于保存此数据的根级别。这一切都在连接到我的 Activities/Fragments
的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 实例和 return 特定格式的时间戳。 Constants.FIREBASE_URL 只是 https://example.firebaseio.com/
编辑 2:
mMainFirebaseInstance = FireBaseHelper.getItemsBase("1234");
可由
替换mMainFirebaseInstance = new Firebase("https://example.firebaseio.com/item_data/items/1234/data");
可能的时间戳是:
2015-06-22 23:12:24
saveDataToServer 中使用的 id 是从 ValueEventListener 中提供给我的快照中检索的。例如:
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);
}
...
}
编辑 3:
Android: 5.0
火力地堡:2.3.0+
Java: 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://Whosebug.firebaseio.com/30987733");
Map<String, Object> children = new HashMap<>();
children.put("updated_at", new Date().toString());
children.put("is_handled", true);
ref.updateChildren(children);
}
重现上述代码的步骤:
- 进入飞行模式
- 启动应用程序(以便它调用
updateChildren
- 终止应用程序
- 上网
- 启动应用程序
在我的测试中,第二个更新 确实 写入了 Firebase。但是 Android 日志中写了一条警告:
W/RepoOperation﹕ Persisted write at /30987733 failed: FirebaseError: Invalid token in path