Firestore: キャッシュからデータを取得する

Fish
Fish
Sep 9, 2018 · 2 min read

DocumentReference の関数 get は第一引数に任意で GetOption を設定できます。

class DocumentReference implements DocumentReference {
get(options?: GetOptions): Promise<DocumentSnapshot>
}

{ source: 'cache' } ではIndexedDBにあるキャッシュからデータを取得します。

ref.get({ source: 'cache' }).then(snapshot => {
console.log(snapshot) // from cache
})
ref.get({ source: 'server' }).then(snapshot => {
console.log(snapshot) // from server
})

これはIndexedDBにキャッシュが無い状態では使用できないので注意です。

getDocumentFromLocalCache(docKey: DocumentKey): Promise<Document | null>

getDocumentFromLocalCache という関数が呼び出されますが、失敗するとエラーを投げます。

Failed to get document from cache. (However, this document may exist on the server. Run again without setting 'source' in the GetOptions to attempt to retrieve the document from the server.
Fish

Written by

Fish

https://github.com/uufish

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade