Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Realm no realmobject has linker stripped them

Tags:

I am receiving an exception at runtime as follows.

"No realmobject. Has linker stripped them...."

My solution includes a PCL, Android and IOS project with Visual studio Mac and the realm package 1.6.0 installed in each project. I've also checked that Fodyweaver.xml includes the correct reference and all packages have th same version.

When I have the PCL included in the same folder as the solution (i.e like the default multiplatform solution with PCL) everything works ok.

However I moved the PCL project which includes all the realm logic to a separate folder so I can use it across multiple solutions. My solution now includes the PCL from this external folder and the iOS and Android project also reference the realm packages. the app compiles fine but when I run the application it now receives this exception on the first call to use realm.getinstance.

If Input the PCL project back into the same folder as the main solution as originally created it works fine.

can anyone advise a fix for this ?

like image 634
Chris Baxter Avatar asked Sep 05 '17 07:09

Chris Baxter


2 Answers

I've solved the issue now. Firstly I had applied the solution from @sushhangover, but it didn't work straight off.

After some investigation I discovered the compiler was not weaving the classes and realm objects into the library at all.

I simply loaded the library independently of my main solution, removed and reloaded realm packages and Fody, cleaned it all, rebuild All. and then I could see the fodyweaver working properly. I then added the reference back into my main solution and it all works .

like image 78
Chris Baxter Avatar answered Sep 30 '22 00:09

Chris Baxter


This is the same issue I have when placing my RealmObject models into a separate library (PCL or NetStd) as I use a Viper architecture and I share the same model across multiple solutions.

When Realms.Realm.GetInstance(....) is called the Realm initialization assumes the RealmObjects will be in the same assembly or that the assembly containing is already loaded, but they are not in this case. You can tell this is the case as a compiler warning is issued in the assembly build (via the Fody processing) that is calling GetInstance but that does not have any RealmObjects in it:

Warning: Fody/RealmWeaver: Default schema appears to be empty. This is not an error if you don't have any RealmObject inheritors declared. Otherwise it may be a bug with the weaver. (GeneticCancerSelectors)

So I add a static class to my Realm model library:

public static class RealmModel
{
    public static Realms.Realm GetInstance() => GetInstance("");

    public static Realms.Realm GetInstance(string databasePath) => GetInstance(new RealmConfiguration(databasePath));

    public static Realms.Realm GetInstance(RealmConfigurationBase config = null) => Realms.Realm.GetInstance(config);

    public static Task<Realms.Realm> GetInstanceAsync(RealmConfigurationBase config) => Realms.Realm.GetInstanceAsync(config);
}

Now when you need to get a Realm instance, do not call:

Realms.Realm.GetInstance()

But call the one in your Model assembly:

RealmModel.GetInstance()
like image 23
SushiHangover Avatar answered Sep 30 '22 01:09

SushiHangover