在RealmObject外部创建托管的RealmList [英] Creating managed RealmList outside RealmObject

查看:165
本文介绍了在RealmObject外部创建托管的RealmList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个接受ID的 ArrayList 的方法,并返回属于机器的 RealmList 这些ID。

In my app, I have a method that accepts an ArrayList of ID's and returns a RealmList of Machines belonging to these IDs.

public RealmList<Machine> getMachinesById(ArrayList<Long> machineIds) {
    RealmList<Machine> machines = new RealmList<Machine>();
    for (int i = 0; i < machineIds.size(); i++){
        Machine m = getMachineById(machineIds.get(i));
        if (m != null) {
            machines.add(m);
        }
    }
    return machines;
}

getMachineById() function只为特定的id找到正确的机器。

The getMachineById() function just finds the correct machine for a specific id.

我想更多地过滤这个输出,但是,当我试图得到通过 .where() RealmQuery ,我得到一个异常告诉我应该把这个'管理模式'中的RealmList

I want to filter this output some more, however, when I try to get the RealmQuery by doing .where(), I get an Exception telling me I should put this RealmList in 'managed mode'.

Caused by: io.realm.exceptions.RealmException: This method is only available in managed mode
                                                 at io.realm.RealmList.where(RealmList.java:425)

我知道我收到此错误因为这个列表是独立的,不是由Realm管理的。

I'm aware that I get this error because this list is standalone, and not managed by Realm.

可能很重要的是要添加这个函数会被调用很多,因为每次都会触发它我的应用程序中的列表刷新。这意味着(如果可能的话)每次我创建一个新的托管RealmList。

It is probably important to add that this function will be called quite a lot, since it is triggered every time some list in my app refreshes. This would mean that (if possible) every time I'm creating a new managed RealmList.

我的问题:


  • 有没有办法让这个RealmList由Realm管理?

  • 如果可能的话,这个函数经常被调用是一个问题吗?

  • 是否有其他(首选)方法可以实现此目的(ID列表> RealmResults / RealmQuery)

推荐答案


有没有办法让这个RealmList由Realm管理?

Is there any way to let this RealmList be managed by Realm?

是的。有。但是拥有 RealmList 的要点是它应该是 RealmObjects 的字段。例如:

Yes. There is. But the point of having RealmList is it should be a field of an RealmObjects. eg.:

public class Factory {
    RealmList<Machine> machineList;
    // setter & getters
}

Factory factory = new Factory();
RealmList<Machine> machineList = new RealmList<>();
// Add something to the list
factory.setMachines(machineList);
realm.beginTransaction();
Factory managedFactory = realm.copyToRealmOrUpdate(factory);
realm.commitTransaction();

管理意味着它已被持久化Realm。

Managed means it has be persisted Realm.

如果可能的话,这个函数经常被调用是一个问题

If this is possible, is it a problem that this function is being called pretty often

取决于,如果您不需要再次坚持,请参阅答案3.

Depends, if you don't need to persist them again, see answer 3.


是否有其他(首选)方法可以实现此目的( ID列表> RealmResults / RealmQuery)

Is there any other (preferred) way to achieve this (List of IDs > RealmResults/RealmQuery)

在您的情况下,也许您可​​以使用 ReaulResults 相反?例如:

In your case, maybe you can use ReaulResults instead? eg.:

RealmQuery<Machine> query = realm.where(Machine.class);
for (int i = 0; i < machineIds.size(); i++){
    if (i != 0) query = query.or();
    query = query.equalTo("id", machineIds.get(i));
}
RealmResults<Machine> machines = query.findAll();

这篇关于在RealmObject外部创建托管的RealmList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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