Reputation: 51
I have a Xamarin Forms application that uses Realm for our mobile database and we are getting a "System.InvalidOperationException: No RealmObjects. Has linker stripped them?" exception only on Android, and only when you are opening the app outside of a debugging session from Visual Studio.
If you launch the app from Visual Studio with the debugger, then the app works fine and loads Realm properly. If you stop the debugging session and then re-open the app on the device you will get that exception and the app crashes. If you just publish the app to the device from Visual Studio and then launch it you will also see that exception. iOS works fine and does not see this error.
I have tried doing both Debug and Release builds, disabled linking, ProGuard, etc..., updated from Realm 1.2.1 to 1.4.0 and none of those solve the problem.
Our project is structured so that all of our Realm objects are in a separate .NET Standard 1.6 project that is referenced by our platform specific projects. We have Realm 1.4 installed in our platform projects as well as our Realm model project.
I used dotPeek to reflect the output dll from our Realm model project to verify that the generated RealmModuleInitializer.Initialize() method adds all 36 of our RealmObjects to the default types list for the RealmSchema class, so the weaving step should be working properly.
Is there anything else that I can try to get this working on Android outside of a debugging session?
Upvotes: 2
Views: 602
Reputation: 51
Here are some more details in case anybody else runs into this issue.
The Realm schema is processed by a module initializer in the assembly where the Realm objects are defined. The module initializer is supposed to run when any code in that module is loaded for the first time. Since we had pulled out all of our Realm objects into their own project, we needed to execute code from that project prior to creating our Realm instance or else the schema would not be defined. However, the code that we had been running did not have a default constructor so it was falling back into the RealmObject constructor and that was causing the problem. By executing code that did have a default constructor all of our issues went away.
Upvotes: 3