Android: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created

前端 未结 2 965
半阙折子戏
半阙折子戏 2021-01-26 14:15

So inside a IntentService, the app maybe active or inactive , onHandleIntent gets called , where I have placed this below code.This is where I store th

2条回答
  •  庸人自扰
    2021-01-26 14:59

    for (AppItem item : appItems) { 
    

    If appItems contains managed RealmObjects that you obtained from a RealmResults on the UI thread, then accessing them will fail on the background thread.

    realm.executeTransactionAsync((realm) -> { gives you a Realm as parameter that is running on Realm's thread executor, so that must be used to obtain managed RealmResults/RealmObjects inside the transaction.

    So you need to re-query the objects inside realm.executeTransactionAsync if the objects are managed.

                public void execute(Realm realm) {
                    for (AppItem item : realm.where(AppItem.class).findAll()) { 
    

提交回复
热议问题