TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

Updating an array.. #265

Closed robertmaxted closed 8 months ago

robertmaxted commented 9 months ago

Hi Guys, @TobiasBuchholz @vhugogarcia @Gekidoku @AdamEssenmacher

I am trying to update an array of maps but I am getting an error similar to this:

Message "Do not know how to marshal object of type 'System.Collections.Generic.List`1[NameSpace.SomeModel]' to an NSObject"

here is an example Model (to simplify what I am trying to do)..

public class SomeModel
    {
        [FirestoreDocumentId]
        [JsonIgnore]
        public string Id { get; set; }

        [FirestoreProperty("some_array")]
        [JsonProperty("some_array")]
        public List< MapModel > SomeArray { get; set; }
    }

    public class MapModel
    {
        [FirestoreProperty("some_uid_id")]
        [JsonProperty("some_uid_id")]
        public string SomeUid { get; set; }

        [FirestoreProperty("rating")]
        [JsonProperty("rating")]
        public double Rating { get; set; }

        [JsonIgnore]
        public int Position { get; set; }
    }

And here is what I am trying to do:

var path = $"some_collection/some_doc";
var someIDocReference = CrossFirebaseFirestore.Current.GetDocument(path);

someModel = someIDocReference.GetDocumentSnapshotAsync< SomeModel >().Result.Data;

var fieldsToUpdate = new Dictionary<object, object>
                                {
                                    { "some_array", someModel.SomeArray }
                                };

transaction.UpdateData(someIDocReference, fieldsToUpdate);

it is on this line where I get the error:

transaction.UpdateData(someIDocReference, fieldsToUpdate);

Any help would be greatly appreciated!

Kind regards, Rob.

AdamEssenmacher commented 9 months ago

Try using an IList instead of a List.

robertmaxted commented 9 months ago

@AdamEssenmacher Ahh.. I see.. But IList doesn't have all of the methods ie RemoveAll etc.. Is there no way it can be done with List ? Thanks for the quick response Adam, I appreciate it.

robertmaxted commented 9 months ago

@AdamEssenmacher I am trying to run a transaction and update an array of maps within a document.. I must be going wrong here somewhere.

transaction.UpdateData(documentReference, ("some_array", tempList));

Error: Message "Could not convert object of type NameSpace.Models.MapModel to NSObject. Does it extend IFirestoreObject?"

SomeArray is now an IList:

public IList SomeArray { get; set; }

AdamEssenmacher commented 8 months ago

@robertmaxted have you figured it out yet? The error message is hinting that your models should extend IFirestoreObject, and it doesn't look like your models do...

robertmaxted commented 8 months ago

That worked a treat! Thanks Adam I really appreciate it!