Catch up on everthing we announced at this year's Firebase Summit. Learn more

離線訪問數據

Cloud Firestore 支持離線數據持久化。此功能會緩存您的應用正在使用的 Cloud Firestore 數據的副本,以便您的應用可以在設備離線時訪問這些數據。您可以寫入、讀取、偵聽和查詢緩存數據。當設備重新上線時,Cloud Firestore 會將您的應用所做的任何本地更改同步到 Cloud Firestore 後端。

要使用離線持久性,您無需對用於訪問 Cloud Firestore 數據的代碼進行任何更改。啟用離線持久性後,Cloud Firestore 客戶端庫會自動管理在線和離線數據訪問,並在設備重新在線時同步本地數據。

配置離線持久化

初始化 Cloud Firestore 時,您可以啟用或禁用離線持久性:

  • 對於安卓和蘋果平台,默認開啟離線持久化。要禁用持久性,設置PersistenceEnabled選項false
  • 對於 Web,默認情況下禁用離線持久性。要啟用持久性,調用enablePersistence方法。 Cloud Firestore 的緩存不會在會話之間自動清除。因此,如果您的 Web 應用程序處理敏感信息,請確保在啟用持久性之前詢問用戶他們是否在受信任的設備上。

網頁版 9

import { enableIndexedDbPersistence } from "firebase/firestore"; 

enableIndexedDbPersistence(db)
  .catch((err) => {
      if (err.code == 'failed-precondition') {
          // Multiple tabs open, persistence can only be enabled
          // in one tab at a a time.
          // ...
      } else if (err.code == 'unimplemented') {
          // The current browser does not support all of the
          // features required to enable persistence
          // ...
      }
  });
// Subsequent queries will use persistence, if it was enabled successfully

網頁版 8

firebase.firestore().enablePersistence()
  .catch((err) => {
      if (err.code == 'failed-precondition') {
          // Multiple tabs open, persistence can only be enabled
          // in one tab at a a time.
          // ...
      } else if (err.code == 'unimplemented') {
          // The current browser does not support all of the
          // features required to enable persistence
          // ...
      }
  });
// Subsequent queries will use persistence, if it was enabled successfully
迅速
注:本產品不提供watchOS和App剪輯目標。
let settings = FirestoreSettings()
settings.isPersistenceEnabled = true

// Any additional options
// ...

// Enable offline data persistence
let db = Firestore.firestore()
db.settings = settings
目標-C
注:本產品不提供watchOS和App剪輯目標。
FIRFirestoreSettings *settings = [[FIRFirestoreSettings alloc] init];
settings.persistenceEnabled = YES;

// Any additional options
// ...

// Enable offline data persistence
FIRFirestore *db = [FIRFirestore firestore];
db.settings = settings;

爪哇

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
        .setPersistenceEnabled(true)
        .build();
db.setFirestoreSettings(settings);

科特林+KTX

val settings = firestoreSettings {
    isPersistenceEnabled = true
}
db.firestoreSettings = settings

配置緩存大小

啟用持久性後,Cloud Firestore 會緩存從後端收到的每個文檔以供離線訪問。 Cloud Firestore 設置了緩存大小的默認閾值。超過默認值後,Cloud Firestore 會定期嘗試清理舊的、未使用的文檔。您可以配置不同的緩存大小閾值或完全禁用清理過程:

網頁版 9

import { initializeFirestore, CACHE_SIZE_UNLIMITED } from "firebase/firestore";

const firestoreDb = initializeFirestore(app, {
  cacheSizeBytes: CACHE_SIZE_UNLIMITED
});

網頁版 8

firebase.firestore().settings({
    cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
});
迅速
注:本產品不提供watchOS和App剪輯目標。
// The default cache size threshold is 100 MB. Configure "cacheSizeBytes"
// for a different threshold (minimum 1 MB) or set to "FirestoreCacheSizeUnlimited"
// to disable clean-up.
let settings = Firestore.firestore().settings
settings.cacheSizeBytes = FirestoreCacheSizeUnlimited
Firestore.firestore().settings = settings
目標-C
注:本產品不提供watchOS和App剪輯目標。
// The default cache size threshold is 100 MB. Configure "cacheSizeBytes"
// for a different threshold (minimum 1 MB) or set to "kFIRFirestoreCacheSizeUnlimited"
// to disable clean-up.
FIRFirestoreSettings *settings = [FIRFirestore firestore].settings;
settings.cacheSizeBytes = kFIRFirestoreCacheSizeUnlimited;
[FIRFirestore firestore].settings = settings;
  

爪哇


// The default cache size threshold is 100 MB. Configure "setCacheSizeBytes"
// for a different threshold (minimum 1 MB) or set to "CACHE_SIZE_UNLIMITED"
// to disable clean-up.
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
        .setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
        .build();
db.setFirestoreSettings(settings);

科特林+KTX


// The default cache size threshold is 100 MB. Configure "setCacheSizeBytes"
// for a different threshold (minimum 1 MB) or set to "CACHE_SIZE_UNLIMITED"
// to disable clean-up.
val settings = FirebaseFirestoreSettings.Builder()
        .setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
        .build()
db.firestoreSettings = settings

聽離線數據

當設備處於離線狀態時,如果您啟用了離線持久化,當本地緩存的數據發生變化時,您的偵聽器將收到偵聽事件。您可以收聽文檔、集合和查詢。

要檢查是否正在接收來自服務器或緩存數據,使用fromCache對房地產SnapshotMetadata在快照活動。如果fromCachetrue ,將數據從緩存來了,可能是無效的或不完整的。如果fromCachefalse ,數據是完整的,當前與服務器上的最新更新。

默認情況下,如果僅僅是沒有提出事件SnapshotMetadata改變。如果你依靠fromCache值,指定includeMetadataChanges聽選項,當您附上你聽處理程序。

網頁版 9

import { collection, onSnapshot, where, query } from "firebase/firestore"; 

const q = query(collection(db, "cities"), where("state", "==", "CA"));
onSnapshot(q, { includeMetadataChanges: true }, (snapshot) => {
    snapshot.docChanges().forEach((change) => {
        if (change.type === "added") {
            console.log("New city: ", change.doc.data());
        }

        const source = snapshot.metadata.fromCache ? "local cache" : "server";
        console.log("Data came from " + source);
    });
});

網頁版 8

db.collection("cities").where("state", "==", "CA")
  .onSnapshot({ includeMetadataChanges: true }, (snapshot) => {
      snapshot.docChanges().forEach((change) => {
          if (change.type === "added") {
              console.log("New city: ", change.doc.data());
          }

          var source = snapshot.metadata.fromCache ? "local cache" : "server";
          console.log("Data came from " + source);
      });
  });
迅速
注:本產品不提供watchOS和App剪輯目標。
// Listen to metadata updates to receive a server snapshot even if
// the data is the same as the cached data.
db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener(includeMetadataChanges: true) { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error retreiving snapshot: \(error!)")
            return
        }

        for diff in snapshot.documentChanges {
            if diff.type == .added {
                print("New city: \(diff.document.data())")
            }
        }

        let source = snapshot.metadata.isFromCache ? "local cache" : "server"
        print("Metadata: Data fetched from \(source)")
}
目標-C
注:本產品不提供watchOS和App剪輯目標。
// Listen to metadata updates to receive a server snapshot even if
// the data is the same as the cached data.
[[[db collectionWithPath:@"cities"] queryWhereField:@"state" isEqualTo:@"CA"]
    addSnapshotListenerWithIncludeMetadataChanges:YES
    listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
      if (snapshot == nil) {
        NSLog(@"Error retreiving snapshot: %@", error);
        return;
      }
      for (FIRDocumentChange *diff in snapshot.documentChanges) {
        if (diff.type == FIRDocumentChangeTypeAdded) {
          NSLog(@"New city: %@", diff.document.data);
        }
      }

      NSString *source = snapshot.metadata.isFromCache ? @"local cache" : @"server";
      NSLog(@"Metadata: Data fetched from %@", source);
    }];

爪哇

db.collection("cities").whereEqualTo("state", "CA")
        .addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot querySnapshot,
                                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w(TAG, "Listen error", e);
                    return;
                }

                for (DocumentChange change : querySnapshot.getDocumentChanges()) {
                    if (change.getType() == Type.ADDED) {
                        Log.d(TAG, "New city:" + change.getDocument().getData());
                    }

                    String source = querySnapshot.getMetadata().isFromCache() ?
                            "local cache" : "server";
                    Log.d(TAG, "Data fetched from " + source);
                }

            }
        });

科特林+KTX

db.collection("cities").whereEqualTo("state", "CA")
        .addSnapshotListener(MetadataChanges.INCLUDE) { querySnapshot, e ->
            if (e != null) {
                Log.w(TAG, "Listen error", e)
                return@addSnapshotListener
            }

            for (change in querySnapshot!!.documentChanges) {
                if (change.type == DocumentChange.Type.ADDED) {
                    Log.d(TAG, "New city: ${change.document.data}")
                }

                val source = if (querySnapshot.metadata.isFromCache)
                    "local cache"
                else
                    "server"
                Log.d(TAG, "Data fetched from $source")
            }
        }

獲取離線數據

如果您在設備離線時獲取文檔,Cloud Firestore 會從緩存中返回數據。

查詢集合時,如果沒有緩存的文檔,則返回空結果。獲取特定文檔時,會返回錯誤。

查詢離線數據

查詢適用於離線持久性。您可以通過直接獲取或通過偵聽來檢索查詢結果,如前幾節所述。您還可以在設備離線時對本地持久數據創建新查詢,但這些查詢最初只會針對緩存的文檔運行。

禁用和啟用網絡訪問

您可以使用以下方法禁用 Cloud Firestore 客戶端的網絡訪問權限。當禁用網絡訪問時,所有快照偵聽器和文檔請求都會從緩存中檢索結果。寫入操作將排隊,直到重新啟用網絡訪問。

網頁版 9

import { disableNetwork } from "firebase/firestore"; 

await disableNetwork(db);
console.log("Network disabled!");
// Do offline actions
// ...

網頁版 8

firebase.firestore().disableNetwork()
    .then(() => {
        // Do offline actions
        // ...
    });
迅速
注:本產品不提供watchOS和App剪輯目標。
Firestore.firestore().disableNetwork { (error) in
    // Do offline things
    // ...
}
目標-C
注:本產品不提供watchOS和App剪輯目標。
[[FIRFirestore firestore] disableNetworkWithCompletion:^(NSError *_Nullable error) {
  // Do offline actions
  // ...
}];

爪哇

db.disableNetwork()
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // Do offline things
                // ...
            }
        });

科特林+KTX

db.disableNetwork().addOnCompleteListener {
    // Do offline things
    // ...
}

使用以下方法重新啟用網絡訪問:

網頁版 9

import { enableNetwork } from "firebase/firestore"; 

await enableNetwork(db);
// Do online actions
// ...

網頁版 8

firebase.firestore().enableNetwork()
    .then(() => {
        // Do online actions
        // ...
    });
迅速
注:本產品不提供watchOS和App剪輯目標。
Firestore.firestore().enableNetwork { (error) in
    // Do online things
    // ...
}
目標-C
注:本產品不提供watchOS和App剪輯目標。
[[FIRFirestore firestore] enableNetworkWithCompletion:^(NSError *_Nullable error) {
  // Do online actions
  // ...
}];

爪哇

db.enableNetwork()
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // Do online things
                // ...
            }
        });

科特林+KTX

db.enableNetwork().addOnCompleteListener {
    // Do online things
    // ...
}