领域未获取数据 [英] realm not fetching data

查看:60
本文介绍了领域未获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用领域时遇到问题.

I have problem working with realm.

findAll和findAllAsync不会从领域返回任何数据.

findAll and findAllAsync doesnot return any data from realm.

我正在像这样从主线程更新领域对象.

I am updating realm object from main thread like this.

public void updatePhoto(final int ticketID) {
    realm.beginTransaction();
    RealmResults ticketPhotos =  realm.where(TicketPhoto.class).equalTo("TicketID", ticketID).findAll();`           
    for (TicketPhoto ticketPhoto : ticketPhotos) { 
        ticketPhoto.IsModified = true;
    }
    realm.commitTransaction(); 
} '$'

同时,每五分钟运行一次后台服务,并不断检查是否具有IsModified标志为true的任何对象.从我的后台服务(IntentService),正在使用AsyncTask,并在doInBackground中,正在尝试获取那些IsModified记录,并且我认为领域应该选择这些记录并用我的服务器进行更新.使用以下代码从领域获取数据.

At same time one background service is running for every five minutes and keeps checking for any objects having IsModified flag as true. From my background service(IntentService), am using AsyncTask and in doInBackground, am trying to get those IsModified records and I assume realm should pick those records and update with my server. Used the below code to get data from realm.

public RealmResults getTicketPhotosToSave (){
    return realm.where(TicketPhoto.class)
           .equalTo("IsModified", true)
           .findAll(); 
}

当我仍处于同一照片活动中,我将照片保存到IsModified标志为true的领域时,后台服务中的领域不会选择这些记录.但是,当我销毁该应用程序并再次运行它时,服务现在正在选择这些记录.不知道这里是否做错了.

When am still in the same Photo activity where I save photo to realm with IsModified flag as true, realm in background service is not picking those records. But when I destroy the app and just run it again, service is now picking those records. Am not sure if am doing something wrong here.

推荐答案

有效.谢谢您的支持

现在我明白,除非我们在同一线程上更新数据,否则无需关闭领域. 当我们需要访问不同线程中的那些更改时,总是需要关闭领域. 由于后台线程始终需要访问所有对象,因此在访问数据之前,我们必须关闭和打开领域.

Now I understood that unless we are updating the data on same thread, no need to close realm. We need to close realm always when we need access to those changes in different thread . Since background thread always needs access to all objects, we have to close and open realm just before accessing data.

因此,在访问/查询数据之前,我按照@Zhuinden的建议刷新了领域(领域没有获取数据) 然后是realm.Close().之后,我要创建实例(realm = Realm.getDefaultInstance();)

So before accessing/querying data, I am refreshing realm as @Zhuinden suggested ( realm not fetching data) and then realm.Close(). After this I am creating instance (realm = Realm.getDefaultInstance(); )

这篇关于领域未获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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