Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidCastExceptionin CollectionViewRenderer #47

Closed
meierhoeferjannis opened this issue Dec 9, 2021 · 2 comments
Closed

System.InvalidCastExceptionin CollectionViewRenderer #47

meierhoeferjannis opened this issue Dec 9, 2021 · 2 comments

Comments

@meierhoeferjannis
Copy link

  • OS: iOS
  • Device: IPhone Simulator (IPhone 12)
  • Sdk vervion: iOS 15
  • Xamarin.Forms: 5.0.0.2012

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<Realms.NotifiableObjectHandleBase.CollectionChangeSet>.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 added a commit to meierhoeferjannis/Sharpnado.CollectionView that referenced this issue Dec 16, 2021
For Details see issue roubachof#47 System.InvalidCastExceptionin CollectionViewRenderer
@meierhoeferjannis
Copy link
Author

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants