0

This occurs at the first call to Realm.GetInstance. Happens only on iOS - while on Android it works fine. The Realm version is 0.77.2.

This is a Xamarin project with MvvmCross and ReactiveUI, but most likely this doesn't matter.

The call to Realm.GetInstance happens a bit late - while similar to Android - after MvvmCross is bootstrapped and at least one screen is displayed.

The error is:

System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/Assembly.cs:402 
  at Realms.RealmSchema.<BuildDefaultSchema>m__3 (System.Reflection.Assembly a) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<SelectManyIterator>c__Iterator2`2[TSource,TResult].MoveNext () [0x00059] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:423 
  at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x00062] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:147 
  at Realms.RealmSchema.CreateSchemaForClasses (IEnumerable`1 classes, Realms.SchemaHandle schemaHandle) [0x00025] in <filename unknown>:0 
  at Realms.RealmSchema.BuildDefaultSchema () [0x00071] in <filename unknown>:0 
  at System.Lazy`1[T].CreateValue () [0x00075] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/Lazy.cs:437 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113 
Marcel N.
  • 13,726
  • 5
  • 47
  • 72
  • 1
    What is `Linker behavior` set to on that iOS build? – SushiHangover Sep 07 '16 at 09:08
  • Yes, it is `Don't link`. What's the recommended option? Link all assemblies or SDK assemblies only? – Marcel N. Sep 07 '16 at 09:10
  • I thought maybe it is linking out some your classes since it is a `ReflectionTypeLoadException`... it is not the iOS10 Beta Cycle8 from Xamarin is it? – SushiHangover Sep 07 '16 at 09:17
  • @SushiHangover: It is. That's what was happening. The don't link option linked out I think all of my `RealmObject`s. If you want you can post as an answer and I'll accept it. – Marcel N. Sep 07 '16 at 09:26

2 Answers2

1

Since it is a reflection issue (ReflectionTypeLoadException), I would have to assume the 'Xamarin.iOS` linker is removing some classes.

You can manually add the [Xamarin.iOS.Foundation.Preserve] attribute to those classes (or members) so they are not linked out as you will probably want to set the Linker to Link all or Link Only SDK in your release builds to reduce the final .ipa size.

You can apply this attribute on every member of a type, or on the type itself. If you want to preserve the whole type, you can use the syntax [Preserve (AllMembers = true)] on the type.

Sometimes you want to preserve certain members, but only if the containing type was preserved. In those cases, use [Preserve (Conditional=true)]

Ref: Preserving Code

Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
1

We attempt to add a [Preserve (AllMembers = true)] attribute automatically to your classes that descend from RealmObject so you should not have to manually add them as @SushiHangover suggests.

So it sounds like this is not happening in the Fody weaving step. That might be a bug. It may also be that the way we build our PreserveAttribute is not compatible with the one in this version of IOS so is not acting to preserve

I find the naming convention for linking on Xamarin very counter-intuitive. The names make more sense if you substitute the words Attempt Stripping Unused for Link eg: Don't Attempt Stripping Unused

Do you have your own constructors on your RealmObject subclasses?

Yes

Can you please post the exact IOS and Xamarin Studio versions being used?

The Xamarin version is 4.1.2.18 and the Xamarin.iOS version is 9.8.2.22. I'm using VS 2015 Update 3, not Xamarin Studio.

I have added issue 822 to track this as a fairly high priority in Realm.

Andy Dent
  • 17,578
  • 6
  • 88
  • 115
  • Yes, having the attribute added automatically would be better. I have a default constructor on that particular `RealmObject` where the issue occurred. The Xamarin version is 4.1.2.18 and the Xamarin.iOS version is 9.8.2.22. I'm using VS 2015 Update 3, not Xamarin Studio. – Marcel N. Sep 07 '16 at 10:07