Realm `access from incorrect thread` error when using shared code between IntentService and AsyncTask (Android)

前端 未结 3 1675
北海茫月
北海茫月 2020-12-31 03:17

I have some code that downloads a \"Current\" object\'s JSON. But this same code needs to be called by an IntentService whenever an alarm goes off (when the app is not runni

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 03:59

    If you use the Realm in IntentService ,you will use new Realm .for example

    Realm.getInstance(RealmConfiguration config) 
    

    I used the Realm in IntentService

    @Override
    protected void onHandleIntent(Intent intent) {
        Realm realm = Realm.getDefaultInstance();
        ...
        realm.close(); // important on background thread
    }
    

提交回复
热议问题