查询包含在其他对象上的领域数据 [英] Query realm data contained on other object

查看:165
本文介绍了查询包含在其他对象上的领域数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是来自以下问题的后续问题:在列表中组织Android Realm数据

This question is a follow-up question from: Organize Android Realm data in lists

由于我们使用的API返回的数据,在领域数据库中进行实际查询几乎是不可能的.相反,我将订购的数据包装在RealmList中,并向其中添加了@PrimaryKey public String id;.

Due to the data returned by the API we use, it's slightly impossible to do an actual query on the realm database. Instead I'm wrapping my ordered data in a RealmList and adding a @PrimaryKey public String id; to it.

因此我们的领域数据如下:

So our realm data looks like:

public class ListPhoto extends RealmObject {
   @PrimaryKey public String id;
   public RealmList<Photo> list; // Photo contains String/int/boolean
}

,只需使用API​​端点作为id,就可以轻松地在Realm DB中读写.

which makes easy to write to and read from the Realm DB by simply using the API endpoint as the id.

因此,对它的典型查询如下:

So a typical query on it looks like:

realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();

这对数据造成了listening/subscribing的轻微开销,因为现在我需要检查listUser.isLoaded()ListUser用作addChangeListener/removeChangeListenerListUser.list作为适配器上的实际数据.

This creates a slightly overhead of listening/subscribing to data because now I need to check listUser.isLoaded() use ListUser to addChangeListener/removeChangeListener and ListUser.list as an actual data on my adapter.

所以我的问题是:

有没有一种方法可以查询此领域以接收RealmResults<Photo>.这样,我可以轻松地在RealmRecyclerViewAdapter中使用此数据,并直接在其上使用侦听器.

Is there a way I can query this realm to receive a RealmResults<Photo>. That way I could easily use this data in RealmRecyclerViewAdapter and use listeners directly on it.

为了进一步说明,我想要以下内容(我知道这不能编译,只是我想要实现的伪代码).

to further clarify, I would like something like the following (I know this doesn't compile, it's just a pseudo-code on what I would like to achieve).

realm
 .where(ListPhoto.class)
      .equalTo("id", id)
      .findFirstAsync()  // get a results of that photo list
 .where(Photo.class)
      .getField("list")
      .findAllAsync(); // get the field "list" into a `RealmResults<Photo>`

编辑最终代码:考虑到ATM不可能直接在查询上执行它,我的最终解决方案是简单地拥有一个适配器,该适配器可以检查数据并在需要时进行订阅.下面的代码:

edit final code: considering it's not possible ATM to do it directly on queries, my final solution was to simply have an adapter that checks data and subscribe if needed. Code below:

public abstract class RealmAdapter
                     <T extends RealmModel, 
                      VH extends RecyclerView.ViewHolder> 
            extends RealmRecyclerViewAdapter<T, VH>
            implements RealmChangeListener<RealmModel> {

   public RealmAdapter(Context context, OrderedRealmCollection data, RealmObject realmObject) {
      super(context, data, true);
      if (data == null) {
         realmObject.addChangeListener(this);
      }
   }

   @Override public void onChange(RealmModel element) {

      RealmList list = null;
      try {
         // accessing the `getter` from the generated class
         // because it can be list of Photo, User, Album, Comment, etc
         // but the field name will always be `list` so the generated will always be realmGet$list
         list = (RealmList) element.getClass().getMethod("realmGet$list").invoke(element);
      } catch (Exception e) {
         e.printStackTrace();
      }

      if (list != null) {
         ((RealmObject) element).removeChangeListener(this);
         updateData(list);
      }
   }
}

推荐答案

首先,您查询ListPhoto,因为它是异步的,因此您必须为结果注册一个侦听器.然后,在该侦听器中,您可以查询结果以获取RealmResult.

First you query the ListPhoto, because it's async you have to register a listener for the results. Then in that listener you can query the result to get a RealmResult.

类似这样的东西

final ListPhoto listPhoto = realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();
listPhoto.addChangeListener(new RealmChangeListener<RealmModel>() {
    @Override
    public void onChange(RealmModel element) {
        RealmResults<Photo> photos = listPhoto.getList().where().findAll();
        // do stuff with your photo results here.


        // unregister the listener.
        listPhoto.removeChangeListeners();
    }
});

请注意,您实际上可以查询RealmList.这就是为什么我们可以调用listPhoto.getList().where()的原因. where()仅表示全部退还".

Note that you can actually query a RealmList. That's why we can call listPhoto.getList().where(). The where() just means "return all".

我无法测试,因为我没有您的代码.您可能需要使用((ListPhoto) element)强制转换element.

I cannot test it because I don't have your code. You may need to cast the element with ((ListPhoto) element).

这篇关于查询包含在其他对象上的领域数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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