Search code examples
androidrealmerror-logging

missing log- and/or exception-messages from realm lib


I'm wondering if anybody did ever seen some loglines or exception messages from the realm DB in his app/project?

Some background: I'm new using realm. So sometimes I'm doing some mistakes (eg. trying to update a DataModel extends RealmObject outside a transaction). Of course these things will not work, but unfortunately I didn't see an error or a crash message of my mistake. In debug mode I can step over the line with my error and nothing happens...

I've tried using Crashlytics as described in the docu, but no reports are submitted.

I am using:

  • io.realm:realm-gradle-plugin:1.1.0 and apply plugin: 'realm-android'
  • Android Studio
  • all realm communication happens in a background thread (never on the ui-/main-thread)

Thank's for every hint.


Solution

  • You're probably running into this issue that was fixed in 1.1.1 (using insertOrUpdate() did not check transaction state), so you should update to 1.1.1.

    It's also worth noting that you should probably handle exceptions on your background threads if there are any, and log them if there are.

    try {
        // do blah
    } catch(Throwable e) {
        Log.e(TAG, "An error occurred", e);
        throw e;
    }
    

    In fact, on background threads, you should probably do this

    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        // do blah
    } catch(Throwable e) {
        Log.e(TAG, "An error occurred", e);
        throw e;
    } finally {
        if(realm != null) {
            realm.close();
        }
    }
    

    Also, if you want to listen in on Realm's logs ( THIS WILL BREAK IN 2.0.0 ), you can provide a RealmLog implementation.

    io.realm.internal.log.RealmLog.add(new io.realm.internal.log.Logger() {
        public void v(String message) {
            Log.v("RealmLog", message);
        }
    
        public void v(String message, Throwable t) {
            Log.v("RealmLog", message, t);
        } 
    
        public void d(String message) {
            Log.d("RealmLog", message);
        }
    
        public void d(String message, Throwable t) {
            Log.d("RealmLog", message, t);
        } 
    
        public void i(String message) {
            Log.i("RealmLog", message);
        }
    
        public void i(String message, Throwable t) {
            Log.i("RealmLog", message, t);
        } 
    
        public void w(String message) {
            Log.w("RealmLog", message);
        }
    
        public void w(String message, Throwable t) {
            Log.w("RealmLog", message, t);
        } 
    
        public void e(String message) {
            Log.e("RealmLog", message);
        }
    
        public void e(String message, Throwable t) {
            Log.e("RealmLog", message, t);
        } 
    });
    

    You might not understand everything it tells you though, it's an internal log, after all.