Firebase persistence, clear Firebase cache

前端 未结 2 416
一生所求
一生所求 2021-02-01 03:29

My app uses Firebase to sync and restore data. I use the setValue:withCompletionBlock: method to insert, update and delete Firebase objects. This method is called w

2条回答
  •  再見小時候
    2021-02-01 04:02

    You can use the snap.metadata.fromCache flag to check if the value is coming from the cache or not. Note that if the cache value and the value from the server match it won't fire your onSnapshot twice unless you add the includeMetadataChanges flag seen below, instead it would only fire it once with the metadata.fromCache flag being set to true.

    db.collection('users').doc(uid).onSnapshot({ includeMetadataChanges: true }, (snap) => {
       if (!snap.metadata.fromCache) {
            const user = snap.data();
            // Code here
        }
    })
    

提交回复
热议问题