android - Realm doesn't write the data in IntentService -
i have realm database in app. when app active have no problem writing data if start intentservice , close app, have nothing in realm database.
my intentservice:
@override protected void onhandleintent(intent intent) { realmresults<hashtagobject> hashtags; subscriberobject subscriber = new subscriberobject(); realm realmforthisthread = realm.getdefaultinstance(); hashtags = realmforthisthread.where(hashtagobject.class).findall(); for(int i=0;i<hashtags.size();i++){ getrecentlytag(constants.api_url); } realmforthisthread.begintransaction(); (map.entry entry : hm.entryset()) { if(realmforthisthread.where(subscriberobject.class) .equalto(subscriberobject.id,entry.getkey().tostring()).findall().isempty()) { subscriber.setid(entry.getkey().tostring()); subscriber.setusername(entry.getvalue().tostring()); subscriber.setdate(mdate.gettime()); realmforthisthread.insert(subscriber); } } realmforthisthread.committransaction(); realmforthisthread.close(); }
private void getrecentlytag(string path,final string tagname){
httpurlconnection urlconnection = null; try { thread.sleep(10000); url url = new url(path); urlconnection = (httpurlconnection) url .openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setdoinput(true); urlconnection.connect(); string response = tools.streamtostring(urlconnection .getinputstream()); jsonobject jsonobj = (jsonobject) new jsontokener(response) .nextvalue(); for(int i=0;i<jsonobj.getjsonarray("data").length();i++) { jsonobject json = (jsonobject) jsonobj.getjsonarray("data").get(i); hm.put(json.getjsonobject("user").getstring("id"),json.getjsonobject("user").getstring("username")); } }catch(exception exc){ exc.printstacktrace(); }finally { if(urlconnection!=null){ try{ urlconnection.disconnect(); }catch(exception e){ e.printstacktrace(); } } } }
i using:
classpath "io.realm:realm-gradle-plugin:2.0.2" // top-level build file compile 'io.realm:android-adapters:1.3.0' //app gradle
starts intentservice in fragment like:
@onclick(r.id.find_users) public void startclick(){ intent intent = new intent(getactivity(), subscribersgathering.class); getactivity().startservice(intent); }
realm cofigs in application class:
realm.init(this); realmconfiguration realmconfiguration = new realmconfiguration.builder() .deleterealmifmigrationneeded() .build(); realm.setdefaultconfiguration(realmconfiguration);
Comments
Post a Comment