5

Firestore로 시작합니다. 필자는 오프라인 데이터 지속성에 대한 문서와 튜토리얼을 읽었지만 내용이 수정되지 않은 경우에도 Firestore가 데이터를 다시 다운로드하는지 명확하게 밝히지 않았습니다. 예를 들어, 결과가 일주일에 한 번 업데이트되고 변경 사항이 적용될 때까지 콘텐츠를 다시 다운로드 할 필요가없는 쿼리가있는 경우 코드 작성 효율성 측면에서 가장 좋은 방법은 무엇입니까? ? 감사합니다.Firestore - 온라인 콘텐츠 업데이트까지 캐시 사용

답변

0

당신은 당신의 쿼리를들을 수있는 "스냅 샷 리스너"API를 사용하려면 :

db.collection("cities").where("state", "==", "CA") 
    .onSnapshot(function(querySnapshot) { 
     var cities = []; 
     querySnapshot.forEach(function(doc) { 
      cities.push(doc.data().name); 
     }); 
     console.log("Current cities in CA: ", cities.join(", ")); 
    }); 

이 수신기를 연결 처음 경우 FireStore에 액세스 : 여기 https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection

는 예를 들어 일부 자바 스크립트입니다 네트워크는 모든 결과를 사용자 쿼리에 다운로드하고 예상대로 쿼리 스냅 샷을 제공합니다.

동일한 수신기를 두 번 연결하고 오프라인 지속성을 사용하는 경우 청취자는 캐시의 결과와 즉시 해고됩니다. 다음은 결과가 캐시 또는 로컬에서 경우 감지 할 수있는 방법은 다음과 같습니다 캐시 된 결과를 얻을 후, 경우 FireStore가 쿼리 결과에 어떤 변화가 있는지 확인하기 위해 서버를 확인합니다

db.collection("cities").where("state", "==", "CA") 
    .onSnapshot({ includeQueryMetadataChanges: true }, function(snapshot) { 
     snapshot.docChanges.forEach(function(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); 
     }); 
    }); 

. 그렇다면 변경 사항이있는 다른 스냅 샷이 표시됩니다. https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/QueryListenOptions

:

당신이 당신의 쿼리를 발행 할 때 QueryListenOptions을 사용할 수 있습니다 (어떤 문서 snapshot.metadata.fromCache 변경을 변경할 수 없지만, 예를 들어) 메타 데이터 만 포함하는 변경 통지 할 경우

관련 문제