簡體   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