本文整理汇总了Java中com.google.firebase.firestore.EventListener类的典型用法代码示例。如果您正苦于以下问题:Java EventListener类的具体用法?Java EventListener怎么用?Java EventListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EventListener类属于com.google.firebase.firestore包,在下文中一共展示了EventListener类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: subscribe
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
@Override
public void subscribe(final ObservableEmitter<DocumentSnapshot> emitter) throws Exception {
final EventListener<DocumentSnapshot> listener = new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if (!emitter.isDisposed()) {
if (e == null) {
emitter.onNext(documentSnapshot);
} else {
emitter.onError(e);
}
}
}
};
registration = documentReference.addSnapshotListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
registration.remove();
}
}));
}
示例2: listenForUsers
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenForUsers() {
// [START listen_for_users]
// Listen for users born before 1900.
//
// You will get a first snapshot with the initial results and a new
// snapshot each time there is a change in the results.
db.collection("users")
.whereLessThan("born", 1900)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
Log.d(TAG, "Current users born before 1900: " + snapshots);
}
});
// [END listen_for_users]
}
示例3: listenToDocument
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenToDocument() {
// [START listen_document]
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");
}
}
});
// [END listen_document]
}
示例4: listenToDocumentLocal
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenToDocumentLocal() {
// [START listen_document_local]
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");
}
}
});
// [END listen_document_local]
}
示例5: detachListener
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void detachListener() {
// [START detach_listener]
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(
new EventListener<QuerySnapshot>() {
// [START_EXCLUDE]
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
// ...
}
// [END_EXCLUDE]
});
// ...
// Stop listening to changes
registration.remove();
// [END detach_listener]
}
示例6: handleListenErrors
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void handleListenErrors() {
// [START handle_listen_errors]
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());
}
}
}
});
// [END handle_listen_errors]
}
示例7: offlineListen
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void offlineListen(FirebaseFirestore db) {
// [START offline_listen]
db.collection("cities").whereEqualTo("state", "CA")
.addSnapshotListener(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);
}
}
});
// [END offline_listen]
}
示例8: subscribe
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
@Override
public void subscribe(final ObservableEmitter<QuerySnapshot> emitter) throws Exception {
final EventListener<QuerySnapshot> listener = new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot querySnapshot, FirebaseFirestoreException e) {
if (!emitter.isDisposed()) {
if (e == null) {
emitter.onNext(querySnapshot);
} else {
emitter.onError(e);
}
}
}
};
registration = query.addSnapshotListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
registration.remove();
}
}));
}
示例9: subscribe
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
@Override
public void subscribe(final ObservableEmitter<DocumentChange> emitter) throws Exception {
final EventListener<QuerySnapshot> listener = new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot querySnapshot, FirebaseFirestoreException e) {
if (!emitter.isDisposed()) {
if (e == null) {
for (DocumentChange change : querySnapshot.getDocumentChanges()) {
emitter.onNext(change);
}
} else {
emitter.onError(e);
}
}
}
};
registration = query.addSnapshotListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
registration.remove();
}
}));
}
示例10: listenToMultiple
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenToMultiple() {
// [START listen_multiple]
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 (DocumentSnapshot doc : value) {
if (doc.get("name") != null) {
cities.add(doc.getString("name"));
}
}
Log.d(TAG, "Current cites in CA: " + cities);
}
});
// [END listen_multiple]
}
示例11: listenToDiffs
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenToDiffs() {
// [START listen_diffs]
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;
}
}
}
});
// [END listen_diffs]
}
示例12: listenState
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
private void listenState() {
// [START listen_state]
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()) {
if (dc.getType() == Type.ADDED) {
Log.d(TAG, "New city: " + dc.getDocument().getData());
}
}
if (!snapshots.getMetadata().isFromCache()) {
Log.d(TAG, "Got initial state.");
}
}
});
// [END listen_state]
}
示例13: startUpdates
import com.google.firebase.firestore.EventListener; //导入依赖的package包/类
public void startUpdates() {
String deviceId = "my-device-id";
final DocumentReference reference = db.collection("readings").document(deviceId);
// Listen to the document, including metadata changes so we get notified
// when writes have propagated to the server.
DocumentListenOptions options = new DocumentListenOptions().includeMetadataChanges();
mRegistration = reference.addSnapshotListener(options, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "onEvent:error", e);
}
if (documentSnapshot != null && !documentSnapshot.getMetadata().hasPendingWrites()) {
Log.d(TAG, "Got server snapshot.");
mReadyForUpdate = true;
}
}
});
// On a regular interval, attempt to update the document. Only perform the update
// if we have gotten a snapshot from the server since the last update, which means
// we are ready to update again.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mReadyForUpdate) {
Log.d(TAG, "Updating sensor data");
// Update the document
Map<String, Object> updates = new HashMap<>();
updates.put("temperature", getCurrentTemperature());
updates.put("timestamp", FieldValue.serverTimestamp());
reference.set(updates, SetOptions.merge());
// Mark 'ready for update' as false until we get the next snapshot from the server
mReadyForUpdate = false;
} else {
Log.d(TAG, "Not ready for update");
}
// Schedule the next update
mHandler.postDelayed(this, UPDATE_INTERVAL_MS);
}
}, UPDATE_INTERVAL_MS);
}