Android - Using realm in non activity class -


i creating application android. using realm database. realm working fine in whole app. want insert data when "notificationextender" class (this receive , customise onesignal notification). leads outofmemory error how can handle this.

my sample code: in notification class doing

realmlibrary.insertmessagetodbfromnotificationreceiver(                                 jsonobject.getstring("data"),                                 jsonobject.getstring("groupkey"),                                 jsonobject.getstring("groupkey"),                                  mcontext); 

and in realmlibrary class:

realm realm = getrealm(applicationcontext);  realm.begintransaction();     chatmessages chatmsg = realm.where(chatmessages.class)             .equalto("guid", guid).findfirst();      int count = 0;      if (chatmsg == null) {         // create new object         chatmsg = realm.createobject(chatmessages.class);          count = 1;         chatmsg.setguid(guid);     } else {      }      realm.committransaction(); 

also getrealm method:

 public static realm getrealm(context context) {          return  realm.getdefaultinstance();      } 

how can handle outof memory. please tell me code leads error. not efficient code..

note : sample code. if details needed pls leave comments.

i have 49 reputation points, can't comment this. idea (maybe stupid, tell me if , i'll delete answer), may want add , use leakcanary , check if have memory leaks:

dependencies {    debugcompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'    releasecompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'    testcompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'  } 

and

public class exampleapplication extends application {    @override public void oncreate() {     super.oncreate();     leakcanary.install(this);   } } 

(use leakcanary.install(getapplication()); if activity doesn't extends application)


Comments