Android领域 - 从服务访问领域对象 [英] Android Realm - Accessing Realm Object from Service

查看:227
本文介绍了Android领域 - 从服务访问领域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的活动中创建的领域对象。我需要能够在我创建的服务中访问此对象。但是我在服务中创建Realm对象时遇到错误

I have a realm object that is created in my activity. I need to be able to access this object within a service that I created. However I'm getting the error when creating the Realm object within the service

        mRealm = Realm.getInstance(getApplicationContext());




java.lang.IllegalStateException:从错误的线程访问Realm。
Realm对象只能在创建它们的线程上访问

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

现在我明白这意味着因为领域对象是在我的活动中创建,我无法从后台线程访问它。但是,除了创建我自己的自定义处理程序线程之外,我找不到一个简单的方法,但这似乎是一种笨重的方式。

Now I understand this means that because the realm object was created on my activity, I cannot access it from a background thread. However, I'm not finding an easy way around this other than creating my own custom Handler Thread but that just seems like a clunky way of doing it.

我在这里遗漏了什么,或者是否真的没有更好的方法可以从不同的线程中访问Realm对象?

Am I missing something here or is there really no better way to be able to access Realm object from within different threads?

更新:

我深入研究了一下,在IntentService中,onHandleIntent方法在一个单独的线程中运行类中的其他方法。因此,我无法创建类级别的Realm实例,并且能够与onHandleIntent方法的内部和外部进行交互。这就是造成线程异常的原因。除了在每个方法中创建一个单独的Realm实例,我需要访问该对象并一遍又一遍地检索它,我认为Ilya Tretyakov的答案是最好的。我可以在构造函数中从域中复制对象,然后在服务的整个生命周期中使用它。任何需要回写Realm对象的方法都需要在该方法中实例化自己的Realm实例。

I dug a little deeper to figure out that in an IntentService, the onHandleIntent method runs in a separate thread than other methods within the class. Therefore, I cannot create a class level Realm instance and be able to interact with that inside and outside of the onHandleIntent method. That is what was causing the thread exception. Aside from creating a separate instance of Realm in each method I need to access the object and retrieving it over and over again, I think Ilya Tretyakov's answer will be best. I can copy the object from realm in my constructor and then work with it throughout the life of the service. Any methods that need to write back to the Realm object will need to instantiate their own Realm instance within that method.

推荐答案

你可以尝试使用 realm.copyFromRealm(youRealmObject); 。这些方法将Realm数据复制到普通的Java对象中,并将它们从Realm中分离出来。

You can try to use realm.copyFromRealm(youRealmObject);. These method copy Realm data into normal Java objects and detaching them from Realm.

以下是使用示例:

youRealmObject = realm.copyFromRealm(youRealmObject);

以下是来自docs的信息:

Here is the information about it from docs:


为已经存在的RealmObject创建一个独立的内存中副本。
这是一个深层副本,将复制所有引用的对象。复制的
对象都与Realm分离,因此它们将不再自动更新
。这意味着复制的对象可能
包含的数据不再与其他托管的Realm
对象一致。 警告:对复制对象的任何更改都可以使用copyToRealmOrUpdate(RealmObject)将
合并回Realm,但是所有字段都将被覆盖
,而不仅仅是那些已更改的字段。这包括对其他对象的
引用,并且可能会覆盖其他线程对
所做的更改。

Makes a standalone in-memory copy of an already persisted RealmObject. This is a deep copy that will copy all referenced objects. The copied object(s) are all detached from Realm so they will no longer be automatically updated. This means that the copied objects might contain data that are no longer consistent with other managed Realm objects. WARNING: Any changes to copied objects can be merged back into Realm using copyToRealmOrUpdate(RealmObject), but all fields will be overridden, not just those that were changed. This includes references to other objects, and can potentially override changes made by other threads.

https://realm.io/docs/java/最新/ api / io / realm / Realm.html#copyFromRealm-E-

这篇关于Android领域 - 从服务访问领域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆