创建架构时出现领域异常 - 仅在 iOS 上

Realm exception when schema is created - on iOS only

这发生在第一次调用 Realm.GetInstance 时。仅在 iOS 上发生 - 而在 Android 上它工作正常。领域版本是 0.77.2。

这是一个带有 MvvmCross 和 ReactiveUI 的 Xamarin 项目,但这很可能无关紧要。

Realm.GetInstance 的调用有点晚 - 虽然类似于 Android - 在引导 MvvmCross 并且至少显示一个屏幕之后。

错误是:

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 

因为这是一个反射问题 (ReflectionTypeLoadException),我不得不假设“Xamarin.iOS”链接器正在删除一些 类.

您可以手动将 [Xamarin.iOS.Foundation.Preserve] 属性添加到那些 类(或成员),这样他们就不会像您 可能 想要设置的那样链接出去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)]

参考:Preserving Code

我们尝试将 [Preserve (AllMembers = true)] 属性自动添加到您的 RealmObject 后代的 类,因此您不必像@SushiHangover 建议的那样手动添加它们。

所以听起来这并没有发生在 Fody 编织步骤中。那可能是一个错误。也可能是我们构建 PreserveAttribute 的方式与此版本 IOS 中的不兼容,因此没有采取行动来保留

我发现 Xamarin 上链接的命名约定非常违反直觉。如果将 Attempt Stripping Unused 替换为 Link,这些名称会更有意义,例如:Don't Attempt剥离未使用

你的 RealmObject sub类 上有你自己的构造函数吗?

你能post 确切的 IOS 和正在使用的 Xamarin Studio 版本吗?

Xamarin版本为4.1.2.18,Xamarin.iOS版本为9.8.2.22。我使用的是 VS 2015 Update 3,而不是 Xamarin Studio。

我添加了 issue 822 来跟踪它作为 Realm 中相当高的优先级。