Example to integrate firestore in Xamarin.Forms Andriod/IOS?

So, im trying to implement this libraries inside my project:
Firestore Android:
https://www.nuget.org/packages/Xamarin.Firebase.Firestore/60.1142.0-beta2

Firestore IOS:
https://www.nuget.org/packages/Xamarin.Firebase.iOS.CloudFirestore/

And I've only found 1 example of how to use them both:
https://github.com/yamachu/Sample-Firebase-Cloud-Firestore-Xamarin.Forms

Since I'm having problems when adding/updating in the mapping of .NET objects to Java/NSObject respectively. Does anyone know a library that provides an example of that?
Thanks!

Best Answer

  • albertoha94albertoha94 ✭✭
    Accepted Answer

    Thank you for your replies everyone.
    I was able to find a library that helped me out and would like to share it with you:

    Plugin.CloudFirestore:
    https://github.com/f-miyu/Plugin.CloudFirestore

    It handles both, IOS and Android and it comes with clear examples on how to implement it.
    Would like to see firebase extend to UWP too.

Answers

  • DigitalArtifexDigitalArtifex Member ✭✭
    edited May 23

    @JuniorJiang I don't think it's really applicable to rely on the official firebase documentation for general usage, let alone when the main issue is object mapping. The official documentation shows that the official API does not require such re-mapping. As long as it's a generic Key/Value map, such as a Dictionary, you can simply pass that object to the SetAsync (which doesn't exist at all in the Xamarin Version).

    In order to read a file, the official documentation explains you need to pull a document reference, and call the GetSnapshop method. Again, this functionality does not seem to exist in the Xamarin version.

    I do not believe it is enough to quote official documentation, unless the version you promote is a near-identical implementation with the same functionalities. We need Xamarin specific resources/well commented examples, please.

    Edit: Found the read method.

    Xamarin.Firebase.Firstore:
    document.Get() .AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task obj) => { if (obj.IsSuccessful) { DocumentSnapshot res = obj.GetResult(Class.FromType(typeof(DocumentSnapshot))).JavaCast<DocumentSnapshot>(); recipe = JsonConvert.DeserializeObject<RecipeModel>(res.GetBlob("FileData").ToString()); } else { //TODO: Do something with the error recipe = null; } }));

    Google Official Documentation:
    DocumentReference docRef = db.Collection("cities").Document("BJ"); DocumentSnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Exists) { Console.WriteLine("Document data for {0} document:", snapshot.Id); City city = snapshot.ConvertTo<City>(); } else { Console.WriteLine("Document {0} does not exist!", snapshot.Id); }

  • DigitalArtifexDigitalArtifex Member ✭✭
    edited May 23

    @albertoha94 said:
    ...
    Since I'm having problems when adding/updating in the mapping of .NET objects to Java/NSObject respectively. Does anyone know a library that provides an example of that?
    Thanks!

    If you have not found an easy answer to this, re-mapping TO the required Java map is easy enough as the Java.Util.HashMap class takes a generic map, such as a dictionary, as a constructor argument.

    Dictionary<string,object> detailMap = new Dictionary<string,object> { {"SomeField", objectValue} }; Firestore.Collection(COLLECTION_PATH).Document(DOCUMENT_NAME).Set(new Java.Util.HashMap(detailMap)).Wait();

  • albertoha94albertoha94 Member ✭✭
    Accepted Answer

    Thank you for your replies everyone.
    I was able to find a library that helped me out and would like to share it with you:

    Plugin.CloudFirestore:
    https://github.com/f-miyu/Plugin.CloudFirestore

    It handles both, IOS and Android and it comes with clear examples on how to implement it.
    Would like to see firebase extend to UWP too.

Sign In or Register to comment.