本文整理汇总了Java中com.google.firebase.firestore.Query类的典型用法代码示例。如果您正苦于以下问题:Java Query类的具体用法?Java Query怎么用?Java Query使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Query类属于com.google.firebase.firestore包,在下文中一共展示了Query类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: detachListener
import com.google.firebase.firestore.Query; //导入依赖的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]
}
示例2: simpleQueries
import com.google.firebase.firestore.Query; //导入依赖的package包/类
private void simpleQueries() {
// [START simple_queries]
// Create a reference to the cities collection
CollectionReference citiesRef = db.collection("cities");
// Create a query against the collection.
Query query = citiesRef.whereEqualTo("state", "CA");
// [END simple_queries]
// [START simple_query_capital]
Query capitalCities = db.collection("cities").whereEqualTo("capital", true);
// [END simple_query_capital]
// [START example_filters]
citiesRef.whereEqualTo("state", "CA");
citiesRef.whereLessThan("population", 100000);
citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco");
// [END example_filters]
}
示例3: toQuery
import com.google.firebase.firestore.Query; //导入依赖的package包/类
private Query toQuery(final Filters filters) {
// Construct query basic query
Query query = firestore.collection("restaurants");
if (filters == null) {
query.orderBy("avgRating", Query.Direction.ASCENDING);
} else {
// Category (equality filter)
if (filters.hasCategory()) {
query = query.whereEqualTo(Restaurant.FIELD_CATEGORY, filters.getCategory());
}
// City (equality filter)
if (filters.hasCity()) {
query = query.whereEqualTo(Restaurant.FIELD_CITY, filters.getCity());
}
// Price (equality filter)
if (filters.hasPrice()) {
query = query.whereEqualTo(Restaurant.FIELD_PRICE, filters.getPrice());
}
// Sort by (orderBy with direction)
if (filters.hasSortBy()) {
query = query.orderBy(filters.getSortBy(), filters.getSortDirection());
}
}
/* query could be limited like: query.limit(5) */
return query;
}
示例4: getDefault
import com.google.firebase.firestore.Query; //导入依赖的package包/类
public static Filters getDefault() {
Filters filters = new Filters();
filters.setSortBy(Restaurant.FIELD_AVG_RATING);
filters.setSortDirection(Query.Direction.DESCENDING);
return filters;
}
示例5: getSortDirection
import com.google.firebase.firestore.Query; //导入依赖的package包/类
@Nullable
private Query.Direction getSortDirection() {
String selected = (String) binding.spinnerSort.getSelectedItem();
if (getString(R.string.sort_by_rating).equals(selected)) {
return Query.Direction.DESCENDING;
}
if (getString(R.string.sort_by_price).equals(selected)) {
return Query.Direction.ASCENDING;
}
if (getString(R.string.sort_by_popularity).equals(selected)) {
return Query.Direction.DESCENDING;
}
return null;
}
示例6: deleteCollection
import com.google.firebase.firestore.Query; //导入依赖的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;
});
}
示例7: deleteQueryBatch
import com.google.firebase.firestore.Query; //导入依赖的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();
}
示例8: initFirestore
import com.google.firebase.firestore.Query; //导入依赖的package包/类
private void initFirestore() {
mFirestore = FirebaseFirestore.getInstance();
// Get the 50 highest rated restaurants
mQuery = mFirestore.collection("restaurants")
.orderBy("avgRating", Query.Direction.DESCENDING)
.limit(LIMIT);
/**
* Now we want to listen to the query,
* so that we get all matching documents and are notified of future updates in real time.
* Because our eventual goal is to bind this data to a RecyclerView,
* we need to create a RecyclerView.Adapter class to listen to the data.
*/
}
示例9: onFilter
import com.google.firebase.firestore.Query; //导入依赖的package包/类
@Override
public void onFilter(Filters filters) {
// Construct query basic query
Query query = mFirestore.collection("restaurants");
// Category (equality filter)
if (filters.hasCategory()) {
query = query.whereEqualTo("category", filters.getCategory());
}
// City (equality filter)
if (filters.hasCity()) {
query = query.whereEqualTo("city", filters.getCity());
}
// Price (equality filter)
if (filters.hasPrice()) {
query = query.whereEqualTo("price", filters.getPrice());
}
// Sort by (orderBy with direction)
if (filters.hasSortBy()) {
query = query.orderBy(filters.getSortBy(), filters.getSortDirection());
}
// Limit items
query = query.limit(LIMIT);
// Update the query
mQuery = query;
mAdapter.setQuery(query);
// Set header
mCurrentSearchView.setText(Html.fromHtml(filters.getSearchDescription(this)));
mCurrentSortByView.setText(filters.getOrderDescription(this));
// Save filters
mViewModel.setFilters(filters);
}
示例10: setQuery
import com.google.firebase.firestore.Query; //导入依赖的package包/类
public void setQuery(Query query) {
// Stop listening
stopListening();
// Clear existinkodig data
mSnapshots.clear();
notifyDataSetChanged();
// Listen to new query
mQuery = query;
startListening();
}
示例11: getSortDirection
import com.google.firebase.firestore.Query; //导入依赖的package包/类
@Nullable
private Query.Direction getSortDirection() {
String selected = (String) mSortSpinner.getSelectedItem();
if (getString(R.string.sort_by_rating).equals(selected)) {
return Query.Direction.DESCENDING;
} if (getString(R.string.sort_by_price).equals(selected)) {
return Query.Direction.ASCENDING;
} if (getString(R.string.sort_by_popularity).equals(selected)) {
return Query.Direction.DESCENDING;
}
return null;
}
示例12: deleteQueryBatch
import com.google.firebase.firestore.Query; //导入依赖的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 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();
}
示例13: deleteCollection
import com.google.firebase.firestore.Query; //导入依赖的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 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, new Callable<Void>() {
@Override
public Void call() throws Exception {
// Get the first batch of documents in the collection
Query query = collection.orderBy(FieldPath.documentId()).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(FieldPath.documentId())
.startAfter(last.getId())
.limit(batchSize);
deleted = deleteQueryBatch(query);
}
return null;
}
});
}
示例14: onFilter
import com.google.firebase.firestore.Query; //导入依赖的package包/类
@Override
public void onFilter(Filters filters) {
// Construct query basic query
Query query = mFirestore.collection("restaurants");
// Category (equality filter)
if (filters.hasCategory()) {
query = query.whereEqualTo(Restaurant.FIELD_CATEGORY, filters.getCategory());
}
// City (equality filter)
if (filters.hasCity()) {
query = query.whereEqualTo(Restaurant.FIELD_CITY, filters.getCity());
}
// Price (equality filter)
if (filters.hasPrice()) {
query = query.whereEqualTo(Restaurant.FIELD_PRICE, filters.getPrice());
}
// Sort by (orderBy with direction)
if (filters.hasSortBy()) {
query = query.orderBy(filters.getSortBy(), filters.getSortDirection());
}
// Limit items
query = query.limit(LIMIT);
// Update the query
mAdapter.setQuery(query);
// Set header
mCurrentSearchView.setText(Html.fromHtml(filters.getSearchDescription(this)));
mCurrentSortByView.setText(filters.getOrderDescription(this));
// Save filters
mViewModel.setFilters(filters);
}
示例15: setQuery
import com.google.firebase.firestore.Query; //导入依赖的package包/类
public void setQuery(Query query) {
// Stop listening
stopListening();
// Clear existing data
mSnapshots.clear();
notifyDataSetChanged();
// Listen to new query
mQuery = query;
startListening();
}