本文整理汇总了Java中com.google.firebase.firestore.DocumentSnapshot类的典型用法代码示例。如果您正苦于以下问题:Java DocumentSnapshot类的具体用法?Java DocumentSnapshot怎么用?Java DocumentSnapshot使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DocumentSnapshot类属于com.google.firebase.firestore包,在下文中一共展示了DocumentSnapshot类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllUsers
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
private void getAllUsers() {
// [START get_all_users]
db.collection("users")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
// [END get_all_users]
}
示例2: getAllDocs
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
private void getAllDocs() {
// [START get_multiple_all]
db.collection("cities")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
// [END get_multiple_all]
}
示例3: subscribe
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的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();
}
}));
}
示例4: listenToDocument
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的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]
}
示例5: listenToDocumentLocal
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的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]
}
示例6: deliverUpdate
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
/**
* Delivers the entity state update represented by the given {@link DocumentChange} to
* the observers of the given {@link LiveData}.
*
* @param change the Firestore document change
* @param destination the {@link LiveData} publishing the update
* @param parser the {@link Parser} for the target entity state type
* @param <T> the entity state type
*/
private static <T extends Message>
void deliverUpdate(DocumentChange change,
MutableLiveData<Map<String, T>> destination,
Parser<T> parser) {
final DocumentChange.Type type = change.getType();
final Map<String, T> currentData = destination.getValue();
final Map<String, T> newData = currentData == null
? newHashMap()
: newHashMap(currentData);
final DocumentSnapshot doc = change.getDocument();
final String id = parseMessageId(doc);
final T newMessage = parseMessage(doc, parser);
if (type == ADDED || type == MODIFIED) {
newData.put(id, newMessage);
} else {
throw newIllegalArgumentException("Unexpected document change: %s", type.toString());
}
destination.postValue(newData);
}
示例7: onChildChanged
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
@Override
public void onChildChanged(@NonNull ChangeEventType type,
@NonNull DocumentSnapshot snapshot,
int newIndex,
int oldIndex) {
switch (type) {
case ADDED:
notifyItemInserted(newIndex);
break;
case CHANGED:
notifyItemChanged(newIndex);
break;
case REMOVED:
notifyItemRemoved(oldIndex);
break;
case MOVED:
notifyItemMoved(oldIndex, newIndex);
break;
default:
throw new IllegalStateException("Incomplete case statement");
}
}
示例8: loadDocuments
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
public void loadDocuments (final String p_name) {
Utils.d("Firestore::LoadData");
db.collection(p_name).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
JSONObject jobject = new JSONObject();
try {
for (DocumentSnapshot document : task.getResult()) {
jobject.put(
document.getId(), document.getData());
}
Utils.d("Data: " + jobject.toString());
Utils.callScriptFunc(
"Firestore", "Documents", jobject.toString());
} catch (JSONException e) {
Utils.d("JSON Exception: " + e.toString());
}
} else {
Utils.w("Error getting documents: " + task.getException());
}
}
});
}
示例9: deleteCollection
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
/**
* Delete all documents in a collection. Uses an Executor to perform work on a background
* thread. This does *not* automatically discover and delete subcollections.
*/
private static Task<Void> deleteCollection(final CollectionReference collection,
final int batchSize,
Executor executor) {
// Perform the delete operation on the provided Executor, which allows us to use
// simpler synchronous logic without blocking the main thread.
return Tasks.call(executor, () -> {
// Get the first batch of documents in the collection
Query query = collection.orderBy("__name__").limit(batchSize);
// Get a list of deleted documents
List<DocumentSnapshot> deleted = deleteQueryBatch(query);
// While the deleted documents in the last batch indicate that there
// may still be more documents in the collection, page down to the
// next batch and delete again
while (deleted.size() >= batchSize) {
// Move the query cursor to start after the last doc in the batch
DocumentSnapshot last = deleted.get(deleted.size() - 1);
query = collection.orderBy("__name__")
.startAfter(last.getId())
.limit(batchSize);
deleted = deleteQueryBatch(query);
}
return null;
});
}
示例10: deleteQueryBatch
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
/**
* Delete all results from a query in a single WriteBatch. Must be run on a worker thread
* to avoid blocking/crashing the main thread.
*/
@WorkerThread
private static List<DocumentSnapshot> deleteQueryBatch(final Query query) throws Exception {
QuerySnapshot querySnapshot = Tasks.await(query.get());
WriteBatch batch = query.getFirestore().batch();
for (DocumentSnapshot snapshot : querySnapshot) {
batch.delete(snapshot.getReference());
}
Tasks.await(batch.commit());
return querySnapshot.getDocuments();
}
示例11: onEvent
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
@Override
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
if (e != null) {
setValue(new Resource<>(e));
return;
}
setValue(new Resource<>(snapshot.toObject(type)));
}
示例12: documentToList
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
@NonNull
private List<T> documentToList(QuerySnapshot snapshots) {
final List<T> retList = new ArrayList<>();
if (snapshots.isEmpty()) {
return retList;
}
for (DocumentSnapshot document : snapshots.getDocuments()) {
retList.add(document.toObject(type).withId(document.getId()));
}
return retList;
}
示例13: onRestaurantSelected
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
@Override
public void onRestaurantSelected(DocumentSnapshot restaurant) {
// Go to the details page for the selected restaurant
Intent intent = new Intent(this, RestaurantDetailActivity.class);
intent.putExtra(RestaurantDetailActivity.KEY_RESTAURANT_ID, restaurant.getId());
startActivity(intent);
}
示例14: onEvent
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
@Override
public void onEvent(QuerySnapshot documentSnapshots,
FirebaseFirestoreException e) {
// Handle errors
if (e != null) {
Log.w(TAG, "onEvent:error", e);
return;
}
// Dispatch the event
for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
// Snapshot of the changed document
DocumentSnapshot snapshot = change.getDocument();
switch (change.getType()) {
case ADDED:
onDocumentAdded(change);
break;
case MODIFIED:
onDocumentModified(change);
break;
case REMOVED:
onDocumentRemoved(change);
break;
}
}
onDataChanged();
}
示例15: bind
import com.google.firebase.firestore.DocumentSnapshot; //导入依赖的package包/类
public void bind(final DocumentSnapshot snapshot,
final OnRestaurantSelectedListener listener) {
Restaurant restaurant = snapshot.toObject(Restaurant.class);
Resources resources = itemView.getResources();
// Load image
Glide.with(imageView.getContext())
.load(restaurant.getPhoto())
.into(imageView);
nameView.setText(restaurant.getName());
ratingBar.setRating((float) restaurant.getAvgRating());
cityView.setText(restaurant.getCity());
categoryView.setText(restaurant.getCategory());
numRatingsView.setText(resources.getString(R.string.fmt_num_ratings,
restaurant.getNumRatings()));
priceView.setText(RestaurantUtil.getPriceString(restaurant));
// Click listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onRestaurantSelected(snapshot);
}
}
});
}