1

I am getting the following Exception when I try to save a Realm object inside an IntentService. My guess is that the Service is killed before the Realm save actually happens, but I'm not sure how to fix this?

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.
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • Which Realm version are you using? – beeender Nov 18 '15 at 02:52
  • I guess the problem is you created Realm instance in other threads, but didn't close it when the threads exit. Realm will record handler internally when the Realm instance created in a thread and remove the handler when `Realm.close()` called. When `commitTransaction` called, Realm will send message to every thread's handler which has a Realm instance created. – beeender Nov 20 '15 at 03:17

1 Answers1

1

I believe the problem here is with a different thread that is managed by the IntentService. The system will use the Handler associated with this thread, especially if you also trying to handle some UI(like Toast, sending data to activity, fragment and so one...)

To solve this you could try to open realm in this way:

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

and set

realm.setAutoRefresh(false);
Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16