Android Question Questions about Firebase Cloud Firestore library

asales

Well-Known Member
Licensed User
I get started with the Firebase Cloud Firestore (https://www.b4x.com/android/forum/threads/chargeable-firebase-cloud-firestore.105067/) from @DonManfred and I don't get it works, for now.

This is my first questions about the library:

1 - When I check the FirebaseFirestore library and try to compile, I get his error message: "Maven artifact not found: com.google.protobuf/protobuf-lite"
a) I checked for this artifact in the SDK Manager (proto...) and don't find nothing to install.
b) copy the protobuf jars (from additional library) to the internal libraries path and get the same error.

2 - What is the "Firebase.b4x_excluded" file?
I need to copy this file to any folder?
 

asales

Well-Known Member
Licensed User

Attachments

DonManfred

Expert
Licensed User
copy the com folder to your sdk

pathtosdk\extras\b4a_remote <- Into this folder
edit the installed-components.txt
pathtosdk\extras\b4a_remote\installed-components.txt

and add these lines
B4X:
com.google.protobuf\:protobuf-lite=3.0.1
com.google.guava\:listenablefuture=1.0
com.google.dagger\:dagger=2.17
com.google.auto.value\:auto-value-annotations=1.6.2
install protobuf-lite with SDK Manager

 

Attachments

DonManfred

Expert
Licensed User
I need to copy this file to any folder?
no. It should be inside the jar file from the library.

before using firestore the manifestpart for the Firebase Base was

B4X:
'************ Firebase Base ************
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
'************ Firebase Base (end) ************
It now is (the line with FirebaseAnalytics.Firebase is removed, the new line is aded)
B4X:
'************ Firebase Base ************
'CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseFirestore.Firebase)
'************ Firebase Base (end) ************
 

asales

Well-Known Member
Licensed User
copy the com folder to your sdk (...)
It works. Compile without error. Thanks.

Now I have other questions:
3 - How I can put and get data from a collection?

4 - In the descripton of the library I see the methods (fields) Car and Driver. What are for? How I can use with my own fields?
Car
  • Functions:
    • Initialize (name As String, Category As String, location As com.google.firebase.firestore.GeoPoint, photoUrl As String, DriverRef As com.google.firebase.firestore.DocumentReference)
  • Properties:
    • Category As String
    • Driver As com.google.firebase.firestore.DocumentReference
    • Id As String
    • Location As com.google.firebase.firestore.GeoPoint
    • Name As String
    • NumberPlate As String
    • PhotoUrl As String
Driver
  • Functions:
    • Initialize (id As String, username As String, name As String, Category As String, location As com.google.firebase.firestore.GeoPoint, photoUrl As String, City As String)
  • Properties:
    • Category As String
    • City As String
    • Id As String
    • Location As com.google.firebase.firestore.GeoPoint
    • PhotoUrl As String
    • RealName As String
    • UserName As String

5 - Can you post an example more complete?

Thanks in advance.
 

DonManfred

Expert
Licensed User
Now I have other questions
for which you should create a new thread
3 - How I can put and get data from a collection?
Both are part of the sample.

Reading
B4X:
    Dim driver As CollectionReference = fs.collection("Drivers","DriverCollection")
    If driver <> Null Then
        Log("Setting query to get driver DonManfred")
        driver.whereEqualTo("id","DonManfred").limit(1).fetch("DriverFetch")
        wait for DriverFetch_Snapshot(snap As QuerySnapshot)
        Log($"Driver_Snapshot(${snap.Size},${snap})"$)
        Dim meta As SnapshotMetadata = snap.Metadata
        Dim documents As List = snap.Documents
        'Log(documents)
        If documents.IsInitialized Then
            If documents.Size > 0 Then
                For i = 0 To documents.Size-1
                    Dim docsnap As DocumentSnapshot = documents.Get(i)
                    Log("Query4DonManfred DocumentNo: #"&i&": "&docsnap.Id)
                    Dim data As Map = docsnap.Data
                    Log(data)
                    data.Put("DocumentID",docsnap.Id)
                    data.Put("Reference",docsnap.Reference)
                    glDriverMap.Put(docsnap.Id,data)
                    glDriverMap.Put("DonManfred",docsnap.Id)
                Next
            End If
        Else
            Log("Snapshot does not contain a list of Documents...")
        End If
    End If
writing
B4X:
    Dim loc As GeoPoint
    loc.Initialize(50.833598,6.4411892)
    Dim fahrer As Driver
    fahrer.Initialize("DonManfredTest","DonManfredTest","Manfred Test","Private",loc,"","Düren")
    Log("Adding new Data")
    driver.add2(fahrer) ' Add data to collection
What are for?
These are testclasses/dataclasses. Using your own Dataclasses you can make it all more easier then working with Maps.

B4X:
package de.donmanfred;

import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.GeoPoint;

import anywheresoftware.b4a.BA.ShortName;

@ShortName("Car")
public class Car {
  private String id;
  private DocumentReference Driver;
    private String Name;
  private String Category;
  private String PhotoUrl;
  private String NumberPlate;
    private GeoPoint Location;
  public Car() {
  }
  public Car(String name, String Category, GeoPoint location, String photoUrl, DocumentReference DriverRef) {
      this.Name = name;
      this.Category = Category;
      this.Location = location;
      this.PhotoUrl = photoUrl;
      this.Driver = DriverRef;
  }
  public void Initialize(String name, String Category, GeoPoint location, String photoUrl, DocumentReference DriverRef) {
      this.Name = name;
      this.Category = Category;
      this.Location = location;
      this.PhotoUrl = photoUrl;
      this.Driver = DriverRef;
  }
  public String getPhotoUrl() {
        return PhotoUrl;
    }
    public void setPhotoUrl(String photoUrl) {
        PhotoUrl = photoUrl;
    }
    public String getCategory() {
        return Category;
    }
    public void setCategory(String category) {
        Category = category;
    }
    public GeoPoint getLocation() {
        return Location;
    }
    public void setLocation(GeoPoint location) {
        Location = location;
    }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
  public DocumentReference getDriver() {
        return Driver;
    }
    public void setDriver(DocumentReference DriverRef) {
        Driver = DriverRef;
    }
    public String getNumberPlate() {
        return NumberPlate;
    }
    public void setNumberPlate(String numberPlate) {
        NumberPlate = numberPlate;
    }

}
and Driverclass
B4X:
package de.donmanfred;

import com.google.firebase.firestore.GeoPoint;

import anywheresoftware.b4a.BA.ShortName;

@ShortName("Driver")
public class Driver {
  private String id;
  private String UserName;
    private String RealName;
  private String Category;
  private String City;
    private String PhotoUrl;
    private GeoPoint Location;
  public Driver() {
  }
  public Driver(String id, String username, String name, String Category, GeoPoint location, String photoUrl, String City) {
      this.UserName = username;
      this.id = id;
      this.RealName = name;
      this.Category = Category;
      this.Location = location;
      this.PhotoUrl = photoUrl;
      this.City = City;
  }
  public void Initialize(String id, String username, String name, String Category, GeoPoint location, String photoUrl, String City) {
      this.id = id;
      this.UserName = username;
      this.RealName = name;
      this.Category = Category;
      this.Location = location;
      this.PhotoUrl = photoUrl;
      this.City = City;
  }
  public String getCity() {
        return City;
    }
    public void setCity(String city) {
        City = city;
    }
  public String getPhotoUrl() {
        return PhotoUrl;
    }
    public void setPhotoUrl(String photoUrl) {
        PhotoUrl = photoUrl;
    }
  public String getUserName() {
        return UserName;
    }
    public void setUserName(String userName) {
        UserName = userName;
    }
    public String getRealName() {
        return RealName;
    }
    public void setRealName(String realName) {
        RealName = realName;
    }
    public String getCategory() {
        return Category;
    }
    public void setCategory(String category) {
        Category = category;
    }
    public GeoPoint getLocation() {
        return Location;
    }
    public void setLocation(GeoPoint location) {
        Location = location;
    }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
}
Firebases idea is to use your own Dataclasses for the content of your collections.
You should define a class for any Object you want to store in a Firestore collection.
This will make it easier to filter, query, sorting the results as Firestore will recognize the classes and their properties.

These classes can be easily stored into a collection and firestore is able to cast them when reading to the correct helperclass.

B4X:
 driver.whereEqualTo("id","DonManfred").limit(1).fetch("DriverFetch")
This makes use of the id field of the helperclass.

I would suggest to create your own helperclasses with java. OR just use Maps

Can you post an example more complete?
Everything which is needed is inside the Example.
 
Last edited:
Top