项目作者: varunon9

项目描述 :
An utility class to access firestore database for Android (getOne, create, update, getMany).
高级语言: Java
项目地址: git://github.com/varunon9/firestore-database-utility.git
创建时间: 2018-10-30T03:30:27Z
项目社区:https://github.com/varunon9/firestore-database-utility

开源协议:MIT License

下载


firestore-database-utility

An utility class to access firestore database for Android (getOne, create, update, getMany).

FirestoreDbUtility.java is wrapper class to interact with firestore database in Android. Copy and paste this class along with all other helper classes and interface- FirestoreQueryConditionCode.java, FirestoreQuery.java, FirestoreDbOperationCallback.java in your android project.

Sample Usage-

First of all create an instance of FirestoreDbUtility in your ExampleActivity something like-

FirestoreDbUtility firestoreDbUtility = new FirestoreDbUtility();

Define collection name-

String collectionName = "users"; // collection (table) name

GET unique document from firestore collection

  1. ```
  2. String documentName = "4PkPVt4jGnO5neiKTKbe9I8B9yz2"; // may be uid of FirebaseUser i.e. firebaseUser.getUid()
  3. firestoreDbUtility.getOne(collectionName, documentName,
  4. new FirestoreDbOperationCallback() {
  5. @Override
  6. public void onSuccess(Object object) {
  7. DocumentSnapshot documentSnapshot = (DocumentSnapshot) object;
  8. User user = documentSnapshot.toObject(User.class); // User.java is a model class
  9. System.out.println(user.getEmail()); // do anything with your user object
  10. }
  11. @Override
  12. public void onFailure(Object object) {
  13. }
  14. });
  15. ```

CREATE a document in firestore collection or MERGE if document already exists

  1. ```
  2. User user = new User(); // create an instance of User class and set relevant properties
  3. user.setEmail('email@example.com');
  4. user.setUid('4PkPVt4jGnO5neiKTKbe9I8B9yz2');
  5. firestoreDbUtility.createOrMerge(collectionName,
  6. user.getUid(), user, new FirestoreDbOperationCallback() {
  7. @Override
  8. public void onSuccess(Object object) {
  9. }
  10. @Override
  11. public void onFailure(Object object) {
  12. }
  13. });
  14. ```

UPDATE a document in firestore collection

  1. ```
  2. // set isOnline true of user
  3. Map<String, Object> hashMap = new HashMap<>();
  4. hashMap.put("online", true);
  5. firestoreDbUtility.update(collectionName,
  6. firebaseUser.getUid(), hashMap, new FirestoreDbOperationCallback() {
  7. @Override
  8. public void onSuccess(Object object) {
  9. }
  10. @Override
  11. public void onFailure(Object object) {
  12. }
  13. });
  14. ```

QUERY or getMany documents from firestore collection

  1. ```
  2. GeoPoint lesserGeoPoint = new GeoPoint(12.1, 77.1); // latitude is 12.1 and longitude is 77.1
  3. GeoPoint greaterGeoPoint = new GeoPoint(13.0, 78);
  4. List<FirestoreQuery> firestoreQueryList = new ArrayList<>();
  5. firestoreQueryList.add(new FirestoreQuery(
  6. FirestoreQueryConditionCode.WHERE_LESS_THAN,
  7. "location",
  8. greaterGeoPoint
  9. ));
  10. firestoreQueryList.add(new FirestoreQuery(
  11. FirestoreQueryConditionCode.WHERE_GREATER_THAN,
  12. "location",
  13. lesserGeoPoint
  14. ));
  15. firestoreDbUtility.getMany(collectionName,
  16. firestoreQueryList, new FirestoreDbOperationCallback() {
  17. @Override
  18. public void onSuccess(Object object) {
  19. QuerySnapshot querySnapshot = (QuerySnapshot) object;
  20. for (DocumentSnapshot documentSnapshot: querySnapshot.getDocuments()) {
  21. User user = documentSnapshot.toObject(User.class); // do something with user object
  22. }
  23. }
  24. @Override
  25. public void onFailure(Object object) {
  26. showMessage("Failed to locate nearby travellers.");
  27. }
  28. });
  29. ```