如何在IntentService中保存Realm对象?

时间:2015-11-17 16:48:12

标签: realm android-intentservice

当我尝试在IntentService中保存Realm对象时,我收到以下异常。我的猜测是服务在Realm save实际发生之前被杀死了,但是我不确定如何解决这个问题?

11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue: Handler (android.os.Handler) {febe2b0} sending message to a Handler on a dead thread
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue: java.lang.IllegalStateException: Handler (android.os.Handler) {febe2b0} sending message to a Handler on a dead thread
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.enqueueMessage(Handler.java:631)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.sendMessageAtTime(Handler.java:600)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.sendMessageDelayed(Handler.java:570)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.sendEmptyMessage(Handler.java:519)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at io.realm.BaseRealm.commitTransaction(BaseRealm.java:330)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at io.realm.Realm.commitTransaction(Realm.java:113)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at io.realm.Realm.executeTransaction(Realm.java:1038)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at com.weightbook.database.manager.DataRecordManager.saveDataRecord(DataRecordManager.java:14)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at com.weightbook.sync.DataRecordUtils.updateDataRecordIndexes(DataRecordUtils.java:42)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at com.weightbook.service.ListeningToWearService.onDataChanged(ListeningToWearService.java:164)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at com.google.android.gms.wearable.WearableListenerService$zza$1.run(Unknown Source)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.handleCallback(Handler.java:739)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.Looper.loop(Looper.java:148)
11-17 10:44:12.823 30368-11042/com.weightbook W/MessageQueue:     at android.os.HandlerThread.run(HandlerThread.java:61)
11-17 10:44:12.823 30368-11042/com.weightbook W/REALM: Cannot update Looper threads when the Looper has quit. Use realm.setAutoRefresh(false) to prevent this.

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是由IntentService管理的另一个线程。系统将使用与此线程关联的Handler,特别是如果您还尝试处理某些UI(如Toast,将数据发送到activity,片段等等)。

要解决此问题,您可以尝试以这种方式打开领域:

Realm realm = Realm.getInstance(getApplicationContext());

并设置

realm.setAutoRefresh(false);