Firestore: キャッシュからデータを取得する
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.