AddDefaultTypes should be called before creating a Realm instance

(Ashok Guduru) #1

I’m trying to implement Realm database as local database in my Xamarin Android app.
I’m getting the following error which I’m not able to fix this. What should I do?

Message=AddDefaultTypes should be called before creating a Realm instance with the default schema. If you see this error, please report it to [email protected].

Any help would be appreciated.


(Ashok Guduru) #2

I figured out the reason of this error however not able to know the solution.

I have the following solution structure:

  • App1-UI
    • Lib -> RealmObjects
  • App2-UI
    • Lib -> RealmObjects
  • SharedLib -> RealmObjects

It looks like Realm is building the schema from the assembly by reading classes derived from RealmObject class. My application has more than one assembly having these objects. Realm is failing to read from second assembly once the first assembly is loaded.

Realm needs to be closed and re-opened for second assembly which I’m not able know how to do this. I tried Dispose but not working.


(Nikola Irinchev) #3

Can you try to load the shared lib before using Realm? It should be fine to just access a class/object from the shared lib. Something like this:

// In shared lib
public static class LoadMePlease
    public static readonly int SomeValue = 1;

// In App-UI
public class MyRealmUsingClass
    public void OpenRealm()
        using (var realm = Realm.GetInstance())
            // Should be fine now.

(Ashok Guduru) #4

That did the trick! It is working fine now.


(Ashok Guduru) #5

While it worked initially, it is not working sometimes.
When I debug it works. When I run without debug its failing,
Not sure what to do?

– Thanks

(Nikola Irinchev) #6

What is the code that you used to force load the assembly? Is it possible it’s getting optimized away in a non-debug build?

(Ashok Guduru) #7

Looks like that was the issue. It was a static value.
I changed it by defining dummy types in each of the assemblies and called them by creating instances in the main activity.

Its working now.

– thank you