roubachof / Sharpnado.CollectionView

A performant list view supporting: grid, horizontal and vertical layout, drag and drop, and reveal animations.
The Unlicense
245 stars 30 forks source link

System.InvalidCastExceptionin CollectionViewRenderer #47

Closed meierhoeferjannis closed 2 years ago

meierhoeferjannis commented 2 years ago

Describe the bug Im currently working with Realm (Database) in my Application. In Realm Objects you use an IList for a List of nested Objects:

Example: public class DemoObject:RealmObject { [MapTo("nestedObjects")] public IList<NestedObject> Nested { get; } }

The Problem is that Realm replaces this IList with an IRealmCollection which does not implement IList. Instead it implements IReadOnlyList, IEnumerable, IEnumerable, IReadOnlyCollection, INotifyCollectionChanged, NotifyPropertyChanged. So when you modify the IRealmCollection by adding an Item and the EventHandler CollectionChanged is triggered, the CollectionViewRenderer for iOS tries to cast _itemSource to IList in line 409. The cast then throws the SystemInvalidCastException. Would it be possible to check if _itemSource can be casted to an IList and if not an IEnumerable is used instead?

For example something like:

var count = 0;
if(_itemSource is IList list){
count = list.Count;
}else{
var enumerator = _itemSource.GetEnumerator();
while(enumerator.MoveNext()){
count +=1;
}
if(Control.NumberOfItemsInSection(0) == count){
return;
}

Thanks for any help in advance :)

StackTrace: at Sharpnado.CollectionView.iOS.Renderers.CollectionViewRenderer.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.iOS\Renderers\CollectionViewRenderer.cs:409 at Realms.RealmCollectionBase1[T].RaiseCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\DatabaseTypes\RealmCollectionBase.cs:343 at Realms.RealmCollectionBase1[T].OnChange (Realms.IRealmCollection1[T] sender, Realms.ChangeSet change, System.Exception error) [0x00205] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\DatabaseTypes\RealmCollectionBase.cs:334 at Realms.RealmCollectionBase1[T].Realms.INotifiable.NotifyCallbacks (System.Nullable1[T] changes, System.Nullable1[T] exception) [0x00162] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\DatabaseTypes\RealmCollectionBase.cs:412 at Realms.NotifiableObjectHandleBase.NotifyObjectChanged (System.IntPtr managedHandle, System.IntPtr changes, System.IntPtr exception) [0x00017] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Handles\NotifiableObjectHandleBase.cs:62 at (wrapper native-to-managed) Realms.NotifiableObjectHandleBase.NotifyObjectChanged(intptr,intptr,intptr) at (wrapper managed-to-native) Realms.SynchronizationContextScheduler.scheduler_invoke_function(intptr,bool) at Realms.SynchronizationContextScheduler+Scheduler.b__4_0 (System.Object f_ptr) [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Native\SynchronizationContextScheduler.cs:68 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown ---

at (wrapper managed-to-native) Realms.SynchronizationContextScheduler.scheduler_invoke_function(intptr,bool) at Realms.SynchronizationContextScheduler+Scheduler.b__4_0 (System.Object f_ptr) [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Native\SynchronizationContextScheduler.cs:68 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown ---

at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00013] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:69 at KiloForKilo.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\janni\source\repos\KiloForKilo\KiloForKilo\KiloForKilo.iOS\Main.cs:12

meierhoeferjannis commented 2 years ago

Hi, just wanted to know if you had the time to review my Pull request on this issue. Would be cool if you could do it and release a new fixed version of the collection view :)

roubachof commented 2 years ago

Will be fixed in next version, sorry for the delay :)