आप onSnapshot()
विधि से किसी दस्तावेज़ को सुन सकते हैं। आपके द्वारा प्रदान किए गए कॉलबैक का उपयोग करके एक प्रारंभिक कॉल एकल दस्तावेज़ की वर्तमान सामग्री के साथ तुरंत एक दस्तावेज़ स्नैपशॉट बनाता है। फिर, हर बार सामग्री बदलने पर, एक और कॉल दस्तावेज़ स्नैपशॉट को अपडेट करता है।
Web version 9
import { doc, onSnapshot } from "firebase/firestore"; const unsub = onSnapshot(doc(db, "cities", "SF"), (doc) => { console.log("Current data: ", doc.data()); });
Web version 8
db.collection("cities").doc("SF") .onSnapshot((doc) => { console.log("Current data: ", doc.data()); });
तीव्र
db.collection("cities").document("SF") .addSnapshotListener { documentSnapshot, error in guard let document = documentSnapshot else { print("Error fetching document: \(error!)") return } guard let data = document.data() else { print("Document data was empty.") return } print("Current data: \(data)") }
उद्देश्य सी
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"SF"] addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error fetching document: %@", error); return; } NSLog(@"Current data: %@", snapshot.data); }];
Kotlin+KTX
val docRef = db.collection("cities").document("SF") docRef.addSnapshotListener { snapshot, e -> if (e != null) { Log.w(TAG, "Listen failed.", e) return@addSnapshotListener } if (snapshot != null && snapshot.exists()) { Log.d(TAG, "Current data: ${snapshot.data}") } else { Log.d(TAG, "Current data: null") } }
Java
final DocumentReference docRef = db.collection("cities").document("SF"); docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "Listen failed.", e); return; } if (snapshot != null && snapshot.exists()) { Log.d(TAG, "Current data: " + snapshot.getData()); } else { Log.d(TAG, "Current data: null"); } } });
Dart
final docRef = db.collection("cities").doc("SF"); docRef.snapshots().listen( (event) => print("current data: ${event.data()}"), onError: (error) => print("Listen failed: $error"), );
अक्सर, आप चाहते हैं कि आपका यूआई फायरस्टोर दस्तावेज़ या संग्रह की सामग्री में परिवर्तन पर प्रतिक्रिया करे। आप ऐसा StreamBuilder
विजेट के साथ कर सकते हैं जो फायरस्टार स्नैपशॉट स्ट्रीम का उपभोग करता है:
class UserInformation extends StatefulWidget { @override _UserInformationState createState() => _UserInformationState(); } class _UserInformationState extends State<UserInformation> { final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance.collection('users').snapshots(); @override Widget build(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: _usersStream, builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.hasError) { return const Text('Something went wrong'); } if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Loading"); } return ListView( children: snapshot.data!.docs .map((DocumentSnapshot document) { Map<String, dynamic> data = document.data()! as Map<String, dynamic>; return ListTile( title: Text(data['full_name']), subtitle: Text(data['company']), ); }) .toList() .cast(), ); }, ); } }
जावा
अजगर
सी ++
DocumentReference doc_ref = db->Collection("cities").Document("SF"); doc_ref.AddSnapshotListener( [](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) { if (error == Error::kErrorOk) { if (snapshot.exists()) { std::cout << "Current data: " << snapshot << std::endl; } else { std::cout << "Current data: null" << std::endl; } } else { std::cout << "Listen failed: " << error << std::endl; } });
नोड.जेएस
जाना
पीएचपी
// Not supported in the PHP client library
एकता
DocumentReference docRef = db.Collection("cities").Document("SF"); docRef.Listen(snapshot => { Debug.Log("Callback received document snapshot."); Debug.Log(String.Format("Document data for {0} document:", snapshot.Id)); Dictionary<string, object> city = snapshot.ToDictionary(); foreach (KeyValuePair<string, object> pair in city) { Debug.Log(String.Format("{0}: {1}", pair.Key, pair.Value)); } });
सी#
माणिक
स्थानीय परिवर्तन के लिए घटनाएँ
आपके ऐप में स्थानीय लेखन स्नैपशॉट श्रोताओं को तुरंत आमंत्रित करेगा। यह "विलंबता मुआवजा" नामक एक महत्वपूर्ण विशेषता के कारण है। जब आप कोई लेखन कार्य करते हैं, तो बैकएंड पर डेटा भेजे जाने से पहले आपके श्रोताओं को नए डेटा के साथ सूचित किया जाएगा।
पुनर्प्राप्त दस्तावेज़ों में एक metadata.hasPendingWrites
संपत्ति होती है जो इंगित करती है कि दस्तावेज़ में स्थानीय परिवर्तन हैं जो अभी तक बैकएंड पर नहीं लिखे गए हैं। आप इस संपत्ति का उपयोग अपने स्नैपशॉट श्रोता द्वारा प्राप्त घटनाओं के स्रोत को निर्धारित करने के लिए कर सकते हैं:
Web version 9
import { doc, onSnapshot } from "firebase/firestore"; const unsub = onSnapshot(doc(db, "cities", "SF"), (doc) => { const source = doc.metadata.hasPendingWrites ? "Local" : "Server"; console.log(source, " data: ", doc.data()); });
Web version 8
db.collection("cities").doc("SF") .onSnapshot((doc) => { var source = doc.metadata.hasPendingWrites ? "Local" : "Server"; console.log(source, " data: ", doc.data()); });
तीव्र
db.collection("cities").document("SF") .addSnapshotListener { documentSnapshot, error in guard let document = documentSnapshot else { print("Error fetching document: \(error!)") return } let source = document.metadata.hasPendingWrites ? "Local" : "Server" print("\(source) data: \(document.data() ?? [:])") }
उद्देश्य सी
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"SF"] addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error fetching document: %@", error); return; } NSString *source = snapshot.metadata.hasPendingWrites ? @"Local" : @"Server"; NSLog(@"%@ data: %@", source, snapshot.data); }];
Kotlin+KTX
val docRef = db.collection("cities").document("SF") docRef.addSnapshotListener { snapshot, e -> if (e != null) { Log.w(TAG, "Listen failed.", e) return@addSnapshotListener } val source = if (snapshot != null && snapshot.metadata.hasPendingWrites()) "Local" else "Server" if (snapshot != null && snapshot.exists()) { Log.d(TAG, "$source data: ${snapshot.data}") } else { Log.d(TAG, "$source data: null") } }
Java
final DocumentReference docRef = db.collection("cities").document("SF"); docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "Listen failed.", e); return; } String source = snapshot != null && snapshot.getMetadata().hasPendingWrites() ? "Local" : "Server"; if (snapshot != null && snapshot.exists()) { Log.d(TAG, source + " data: " + snapshot.getData()); } else { Log.d(TAG, source + " data: null"); } } });
Dart
final docRef = db.collection("cities").doc("SF"); docRef.snapshots().listen( (event) { final source = (event.metadata.hasPendingWrites) ? "Local" : "Server"; print("$source data: ${event.data()}"); }, onError: (error) => print("Listen failed: $error"), );
जावा
# Not yet supported in the Java client library
अजगर
// Not yet supported in Python client library
सी ++
DocumentReference doc_ref = db->Collection("cities").Document("SF"); doc_ref.AddSnapshotListener([](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) { if (error == Error::kErrorOk) { const char* source = snapshot.metadata().has_pending_writes() ? "Local" : "Server"; if (snapshot.exists()) { std::cout << source << " data: " << snapshot.Get("name").string_value() << std::endl; } else { std::cout << source << " data: null" << std::endl; } } else { std::cout << "Listen failed: " << error << std::endl; } });
नोड.जेएस
// Not yet supported in the Node.js client library
जाना
// Not yet supported in the Go client library
पीएचपी
// Not supported in the PHP client library
एकता
// Not yet supported in the Unity SDK
सी#
// Not yet supported in the C# client library
माणिक
// Not yet supported in the Ruby client library
मेटाडेटा परिवर्तन के लिए ईवेंट
किसी दस्तावेज़, संग्रह या क्वेरी में परिवर्तनों को सुनते समय, आप अपने श्रोता को प्राप्त होने वाले ईवेंट की ग्रैन्युलैरिटी को नियंत्रित करने के लिए विकल्प पास कर सकते हैं।
डिफ़ॉल्ट रूप से, श्रोताओं को उन परिवर्तनों के बारे में सूचित नहीं किया जाता है जो केवल मेटाडेटा को प्रभावित करते हैं। विचार करें कि जब आपका ऐप एक नया दस्तावेज़ लिखता है तो क्या होता है:
- नए डेटा के साथ एक परिवर्तन घटना तुरंत सक्रिय हो जाती है। दस्तावेज़ को अभी तक बैकएंड पर नहीं लिखा गया है इसलिए "लंबित लेखन" फ़्लैग
true
है। - दस्तावेज़ बैकएंड को लिखा गया है।
- बैकएंड क्लाइंट को सफल लेखन के बारे में सूचित करता है। दस्तावेज़ डेटा में कोई परिवर्तन नहीं है, लेकिन मेटाडेटा परिवर्तन है क्योंकि "लंबित लेखन" फ़्लैग अब
false
है।
यदि आप दस्तावेज़ या क्वेरी मेटाडेटा में परिवर्तन होने पर स्नैपशॉट ईवेंट प्राप्त करना चाहते हैं, तो अपने श्रोता को संलग्न करते समय एक सुनो विकल्प ऑब्जेक्ट पास करें:
Web version 9
import { doc, onSnapshot } from "firebase/firestore"; const unsub = onSnapshot( doc(db, "cities", "SF"), { includeMetadataChanges: true }, (doc) => { // ... });
Web version 8
db.collection("cities").doc("SF") .onSnapshot({ // Listen for document metadata changes includeMetadataChanges: true }, (doc) => { // ... });
तीव्र
// Listen to document metadata. db.collection("cities").document("SF") .addSnapshotListener(includeMetadataChanges: true) { documentSnapshot, error in // ... }
उद्देश्य सी
// Listen for metadata changes. [[[self.db collectionWithPath:@"cities"] documentWithPath:@"SF"] addSnapshotListenerWithIncludeMetadataChanges:YES listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { // ... }];
Kotlin+KTX
// Listen for metadata changes to the document. val docRef = db.collection("cities").document("SF") docRef.addSnapshotListener(MetadataChanges.INCLUDE) { snapshot, e -> // ... }
Java
// Listen for metadata changes to the document. DocumentReference docRef = db.collection("cities").document("SF"); docRef.addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) { // ... } });
Dart
final docRef = db.collection("cities").doc("SF"); docRef.snapshots(includeMetadataChanges: true).listen((event) { // ... });
जावा
// Not yet supported in the Java client library
अजगर
// Not yet supported in Python client library
सी ++
DocumentReference doc_ref = db->Collection("cities").Document("SF"); doc_ref.AddSnapshotListener( MetadataChanges::kInclude, [](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) { /* ... */ });
नोड.जेएस
// Not yet supported the Node.js client library
जाना
// Not yet supported in the Go client library
पीएचपी
// Not supported in the PHP client library
एकता
// Not yet supported in the Unity SDK
सी#
// Not yet supported in the C# client library
माणिक
// Not yet supported in the Ruby client library
एक संग्रह में कई दस्तावेज़ों को सुनें
दस्तावेज़ों की तरह, आप क्वेरी के परिणामों को सुनने के लिए get()
के बजाय onSnapshot()
का उपयोग कर सकते हैं। यह एक क्वेरी स्नैपशॉट बनाता है। उदाहरण के लिए, राज्य CA
के साथ दस्तावेज़ों को सुनने के लिए:
Web version 9
import { collection, query, where, onSnapshot } from "firebase/firestore"; const q = query(collection(db, "cities"), where("state", "==", "CA")); const unsubscribe = onSnapshot(q, (querySnapshot) => { const cities = []; querySnapshot.forEach((doc) => { cities.push(doc.data().name); }); console.log("Current cities in CA: ", cities.join(", ")); });
Web version 8
db.collection("cities").where("state", "==", "CA") .onSnapshot((querySnapshot) => { var cities = []; querySnapshot.forEach((doc) => { cities.push(doc.data().name); }); console.log("Current cities in CA: ", cities.join(", ")); });
तीव्र
db.collection("cities").whereField("state", isEqualTo: "CA") .addSnapshotListener { querySnapshot, error in guard let documents = querySnapshot?.documents else { print("Error fetching documents: \(error!)") return } let cities = documents.map { $0["name"]! } print("Current cities in CA: \(cities)") }
उद्देश्य सी
[[[self.db collectionWithPath:@"cities"] queryWhereField:@"state" isEqualTo:@"CA"] addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error fetching documents: %@", error); return; } NSMutableArray *cities = [NSMutableArray array]; for (FIRDocumentSnapshot *document in snapshot.documents) { [cities addObject:document.data[@"name"]]; } NSLog(@"Current cities in CA: %@", cities); }];
Kotlin+KTX
db.collection("cities") .whereEqualTo("state", "CA") .addSnapshotListener { value, e -> if (e != null) { Log.w(TAG, "Listen failed.", e) return@addSnapshotListener } val cities = ArrayList<String>() for (doc in value!!) { doc.getString("name")?.let { cities.add(it) } } Log.d(TAG, "Current cites in CA: $cities") }
Java
db.collection("cities") .whereEqualTo("state", "CA") .addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "Listen failed.", e); return; } List<String> cities = new ArrayList<>(); for (QueryDocumentSnapshot doc : value) { if (doc.get("name") != null) { cities.add(doc.getString("name")); } } Log.d(TAG, "Current cites in CA: " + cities); } });
Dart
db .collection("cities") .where("state", isEqualTo: "CA") .snapshots() .listen((event) { final cities = []; for (var doc in event.docs) { cities.add(doc.data()["name"]); } print("cities in CA: ${cities.join(", ")}"); });
जावा
अजगर
सी ++
db->Collection("cities") .WhereEqualTo("state", FieldValue::String("CA")) .AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) { if (error == Error::kErrorOk) { std::vector<std::string> cities; std::cout << "Current cities in CA: " << error << std::endl; for (const DocumentSnapshot& doc : snapshot.documents()) { cities.push_back(doc.Get("name").string_value()); std::cout << "" << cities.back() << std::endl; } } else { std::cout << "Listen failed: " << error << std::endl; } });
नोड.जेएस
जाना
पीएचपी
// Not supported in the PHP client library
एकता
Query query = db.Collection("cities").WhereEqualTo("State", "CA"); ListenerRegistration listener = query.Listen(snapshot => { Debug.Log("Callback received query snapshot."); Debug.Log("Current cities in California:"); foreach (DocumentSnapshot documentSnapshot in snapshot.Documents) { Debug.Log(documentSnapshot.Id); } });
सी#
माणिक
स्नैपशॉट हैंडलर हर बार क्वेरी परिणाम बदलने पर एक नया क्वेरी स्नैपशॉट प्राप्त करेगा (अर्थात, जब कोई दस्तावेज़ जोड़ा, हटाया या संशोधित किया जाता है)।
स्नैपशॉट के बीच परिवर्तन देखें
केवल संपूर्ण क्वेरी स्नैपशॉट का उपयोग करने के बजाय क्वेरी स्नैपशॉट के बीच क्वेरी परिणामों में वास्तविक परिवर्तन देखना अक्सर उपयोगी होता है। उदाहरण के लिए, आप कैश को बनाए रखना चाह सकते हैं क्योंकि अलग-अलग दस्तावेज़ जोड़े, निकाले और संशोधित किए जाते हैं।
Web version 9
import { collection, query, where, onSnapshot } from "firebase/firestore"; const q = query(collection(db, "cities"), where("state", "==", "CA")); const unsubscribe = onSnapshot(q, (snapshot) => { snapshot.docChanges().forEach((change) => { if (change.type === "added") { console.log("New city: ", change.doc.data()); } if (change.type === "modified") { console.log("Modified city: ", change.doc.data()); } if (change.type === "removed") { console.log("Removed city: ", change.doc.data()); } }); });
Web version 8
db.collection("cities").where("state", "==", "CA") .onSnapshot((snapshot) => { snapshot.docChanges().forEach((change) => { if (change.type === "added") { console.log("New city: ", change.doc.data()); } if (change.type === "modified") { console.log("Modified city: ", change.doc.data()); } if (change.type === "removed") { console.log("Removed city: ", change.doc.data()); } }); });
तीव्र
db.collection("cities").whereField("state", isEqualTo: "CA") .addSnapshotListener { querySnapshot, error in guard let snapshot = querySnapshot else { print("Error fetching snapshots: \(error!)") return } snapshot.documentChanges.forEach { diff in if (diff.type == .added) { print("New city: \(diff.document.data())") } if (diff.type == .modified) { print("Modified city: \(diff.document.data())") } if (diff.type == .removed) { print("Removed city: \(diff.document.data())") } } }
उद्देश्य सी
[[[self.db collectionWithPath:@"cities"] queryWhereField:@"state" isEqualTo:@"CA"] addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error fetching documents: %@", error); return; } for (FIRDocumentChange *diff in snapshot.documentChanges) { if (diff.type == FIRDocumentChangeTypeAdded) { NSLog(@"New city: %@", diff.document.data); } if (diff.type == FIRDocumentChangeTypeModified) { NSLog(@"Modified city: %@", diff.document.data); } if (diff.type == FIRDocumentChangeTypeRemoved) { NSLog(@"Removed city: %@", diff.document.data); } } }];
Kotlin+KTX
db.collection("cities") .whereEqualTo("state", "CA") .addSnapshotListener { snapshots, e -> if (e != null) { Log.w(TAG, "listen:error", e) return@addSnapshotListener } for (dc in snapshots!!.documentChanges) { when (dc.type) { DocumentChange.Type.ADDED -> Log.d(TAG, "New city: ${dc.document.data}") DocumentChange.Type.MODIFIED -> Log.d(TAG, "Modified city: ${dc.document.data}") DocumentChange.Type.REMOVED -> Log.d(TAG, "Removed city: ${dc.document.data}") } } }
Java
db.collection("cities") .whereEqualTo("state", "CA") .addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "listen:error", e); return; } for (DocumentChange dc : snapshots.getDocumentChanges()) { switch (dc.getType()) { case ADDED: Log.d(TAG, "New city: " + dc.getDocument().getData()); break; case MODIFIED: Log.d(TAG, "Modified city: " + dc.getDocument().getData()); break; case REMOVED: Log.d(TAG, "Removed city: " + dc.getDocument().getData()); break; } } } });
Dart
db .collection("cities") .where("state", isEqualTo: "CA") .snapshots() .listen((event) { for (var change in event.docChanges) { switch (change.type) { case DocumentChangeType.added: print("New City: ${change.doc.data()}"); break; case DocumentChangeType.modified: print("Modified City: ${change.doc.data()}"); break; case DocumentChangeType.removed: print("Removed City: ${change.doc.data()}"); break; } } });
जावा
सी ++
db->Collection("cities") .WhereEqualTo("state", FieldValue::String("CA")) .AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) { if (error == Error::kErrorOk) { for (const DocumentChange& dc : snapshot.DocumentChanges()) { switch (dc.type()) { case DocumentChange::Type::kAdded: std::cout << "New city: " << dc.document().Get("name").string_value() << std::endl; break; case DocumentChange::Type::kModified: std::cout << "Modified city: " << dc.document().Get("name").string_value() << std::endl; break; case DocumentChange::Type::kRemoved: std::cout << "Removed city: " << dc.document().Get("name").string_value() << std::endl; break; } } } else { std::cout << "Listen failed: " << error << std::endl; } });
अजगर
नोड.जेएस
जाना
पीएचपी
// Not supported in the PHP client library
एकता
Query query = db.Collection("cities").WhereEqualTo("State", "CA"); ListenerRegistration listener = query.Listen(snapshot => { foreach (DocumentChange change in snapshot.GetChanges()) { if (change.ChangeType == DocumentChange.Type.Added) { Debug.Log(String.Format("New city: {0}", change.Document.Id)); } else if (change.ChangeType == DocumentChange.Type.Modified) { Debug.Log(String.Format("Modified city: {0}", change.Document.Id)); } else if (change.ChangeType == DocumentChange.Type.Removed) { Debug.Log(String.Format("Removed city: {0}", change.Document.Id)); } } }); } }
सी#
माणिक
प्रारंभिक स्थिति सीधे सर्वर से या स्थानीय कैश से आ सकती है। यदि स्थानीय कैश में स्थिति उपलब्ध है, तो क्वेरी स्नैपशॉट को प्रारंभ में कैश्ड डेटा के साथ पॉप्युलेट किया जाएगा, फिर सर्वर के डेटा के साथ अपडेट किया जाएगा जब क्लाइंट सर्वर की स्थिति के साथ पकड़ा जाएगा।
एक श्रोता को अलग करें
जब आप अपने डेटा को सुनने में रुचि नहीं रखते हैं, तो आपको अपने श्रोता को अलग करना होगा ताकि आपके ईवेंट कॉलबैक को कॉल करना बंद हो जाए। यह क्लाइंट को अपडेट प्राप्त करने के लिए बैंडविड्थ का उपयोग बंद करने की अनुमति देता है। उदाहरण के लिए:
Web version 9
import { collection, onSnapshot } from "firebase/firestore"; const unsubscribe = onSnapshot(collection(db, "cities"), () => { // Respond to data // ... }); // Later ... // Stop listening to changes unsubscribe();
Web version 8
var unsubscribe = db.collection("cities") .onSnapshot(() => { // Respond to data // ... }); // Later ... // Stop listening to changes unsubscribe();
तीव्र
let listener = db.collection("cities").addSnapshotListener { querySnapshot, error in // ... } // ... // Stop listening to changes listener.remove()
उद्देश्य सी
id<FIRListenerRegistration> listener = [[self.db collectionWithPath:@"cities"] addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) { // ... }]; // ... // Stop listening to changes [listener remove];
Kotlin+KTX
val query = db.collection("cities") val registration = query.addSnapshotListener { snapshots, e -> // ... } // ... // Stop listening to changes registration.remove()
Java
Query query = db.collection("cities"); ListenerRegistration registration = query.addSnapshotListener( new EventListener<QuerySnapshot>() { // ... }); // ... // Stop listening to changes registration.remove();
Dart
final collection = db.collection("cities"); final listener = collection.snapshots().listen((event) { // ... }); listener.cancel();
जावा
अजगर
सी ++
// Add a listener Query query = db->Collection("cities"); ListenerRegistration registration = query.AddSnapshotListener( [](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) { /* ... */ }); // Stop listening to changes registration.Remove();
नोड.जेएस
जाना
पीएचपी
// Not supported in the PHP client library
एकता
listener.Stop();
सी#
माणिक
सुनने की त्रुटियों को संभालें
एक सुनना कभी-कभी विफल हो सकता है - उदाहरण के लिए, सुरक्षा अनुमतियों के कारण, या यदि आपने किसी अमान्य क्वेरी को सुनने का प्रयास किया। ( वैध और अमान्य प्रश्नों के बारे में अधिक जानें।) इन विफलताओं को संभालने के लिए, आप अपने स्नैपशॉट श्रोता को संलग्न करते समय एक त्रुटि कॉलबैक प्रदान कर सकते हैं। एक त्रुटि के बाद, श्रोता को कोई और घटनाएँ प्राप्त नहीं होंगी, और आपके श्रोता को अलग करने की कोई आवश्यकता नहीं है।
Web version 9
import { collection, onSnapshot } from "firebase/firestore"; const unsubscribe = onSnapshot( collection(db, "cities"), (snapshot) => { // ... }, (error) => { // ... });
Web version 8
db.collection("cities") .onSnapshot((snapshot) => { // ... }, (error) => { // ... });
तीव्र
db.collection("cities") .addSnapshotListener { querySnapshot, error in if let error = error { print("Error retreiving collection: \(error)") } }
उद्देश्य सी
[[self.db collectionWithPath:@"cities"] addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) { if (error != nil) { NSLog(@"Error retreving collection: %@", error); } }];
Kotlin+KTX
db.collection("cities") .addSnapshotListener { snapshots, e -> if (e != null) { Log.w(TAG, "listen:error", e) return@addSnapshotListener } for (dc in snapshots!!.documentChanges) { if (dc.type == DocumentChange.Type.ADDED) { Log.d(TAG, "New city: ${dc.document.data}") } } }
Java
db.collection("cities") .addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "listen:error", e); return; } for (DocumentChange dc : snapshots.getDocumentChanges()) { if (dc.getType() == Type.ADDED) { Log.d(TAG, "New city: " + dc.getDocument().getData()); } } } });
Dart
final docRef = db.collection("cities"); docRef.snapshots().listen( (event) => print("listener attached"), onError: (error) => print("Listen failed: $error"), );
जावा
अजगर
// Snippet coming soon
सी ++
// Snippet coming soon.
नोड.जेएस
जाना
पीएचपी
// Not supported in the PHP client library
एकता
// Not supported in the Unity SDK.
सी#
// Snippet coming soon
माणिक
आगे क्या होगा
- श्रोताओं को सरल और मिश्रित प्रश्नों के साथ संयोजित करें ।
- प्राप्त दस्तावेजों को आदेश और सीमित करें ।
- श्रोताओं के लिए बिलिंग को समझें ।