TobiasBuchholz / Plugin.Firebase

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

[iOS][Firestore] SetData : System.ArgumentException: Do not know how to marshal object of type 'System.DateTime' to an NSObject #137

Closed tranb3r closed 1 year ago

tranb3r commented 1 year ago

On iOS, when calling SetDataAsync on FireStore Document, I get the following exception if data contains a DateTime value:

System.ArgumentException: Do not know how to marshal object of type 'System.DateTime' to an NSObject
   at Foundation.NSArray.From[Object](Object[] items, Int64 count)
   at Foundation.NSArray.FromObjects(IntPtr count, Object[] items)
   at Foundation.NSDictionary`2[[Foundation.NSString, Microsoft.iOS, Version=16.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065],[Foundation.NSObject, Microsoft.iOS, Version=16.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].FromObjectsAndKeys(Object[] objects, Object[] keys, IntPtr count)
   at Firebase.CloudFirestore.DocumentReference.SetData(Dictionary`2 documentData, DocumentActionCompletionHandler completion)
   at Firebase.CloudFirestore.DocumentReference.SetDataAsync(Dictionary`2 documentData)
   at Plugin.Firebase.iOS.Firestore.DocumentReferenceWrapper.SetDataAsync(Dictionary`2 data, SetOptions options)

I think the SetDataAsync method in DocumentReferenceWrapper (for iOS) should call ToNSObjectDictionary() before passing data to the SDK. This is how it's done in the UpdateDataAsync method (see similar issue #58). What do you think?

TobiasBuchholz commented 1 year ago

Yes, I think you are right, so I will add this to the upcoming version 👍

tranb3r commented 1 year ago

Hi @TobiasBuchholz

I've tested my app again on iOS with version 2.0.1, and I still get the same exception.

I think the DocumentReferenceWrapper code is still missing a call to ToNSObjectDictionary() here: https://github.com/TobiasBuchholz/Plugin.Firebase/blob/8dab85b15cb66fafd35c99b8b4806005f4c7eb6a/src/Firestore/Platforms/iOS/DocumentReferenceWrapper.cs#L24

Could you please double check and maybe fix it in the next version? Of course I can do the PR if you prefer, just let me know.

Thanks!

TobiasBuchholz commented 1 year ago

Yes, a PR would be much appreciated! :)