rosu alin
rosu alin

Reputation: 5830

Realm blocks Gson library

I have an issue with the Realm library. I implemented it in my app, included the library. Started creating some realmObjects, in order to use with my app, and then I tried starting the app, but to my surprise, it got stuck on the first page, which is strange, cause no Realm code was there. After some debugging I found out that it crashed at the following line:

 String json = gson.toJson(user);

Gson is not null. Also I tried and took out realm, to see if that is the issue and it worked without it. This is what crashlytics tells me, after a couple of minutes (it doesn't crashes directly, just sits a while blocked, and after that):

Fatal Exception: java.lang.OutOfMemoryError
OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available  
java.lang.Object.wait (Object.java)
java.lang.Thread.run (Thread.java:818)  
dalvik.system.VMRuntime.concurrentGC (VMRuntime.java)
java.lang.Thread.run (Thread.java:818)
java.lang.Object.wait (Object.java)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
com.crashlytics.android.internal.am.a (SourceFile:44)
com.crashlytics.android.internal.aa.run (SourceFile:13)
java.lang.Thread.run (Thread.java:818)
java.lang.Object.wait (Object.java)
java.util.concurrent.FutureTask.get (FutureTask.java:162)
com.crashlytics.android.v.a (SourceFile:1936)
com.crashlytics.android.v.uncaughtException (SourceFile:307)
java.lang.ThreadGroup.uncaughtException (ThreadGroup.java:693)
java.lang.ThreadGroup.uncaughtException (ThreadGroup.java:690)

EDIT: The Item that I pass to gson (user) is an instance of a class called UserRecord which extends RealmObject. Could it be this is happening because it's a realmObject and not a standard android Object?

Upvotes: 1

Views: 427

Answers (1)

rosu alin
rosu alin

Reputation: 5830

I found the issue: The problem was that I was using:

Gson gson = new Gson();

Instead of what I should have used:

Gson gson = new GsonBuilder()
    .setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    })
    .create();

Upvotes: 1

Related Questions