整理列表中的Android Realm数据 [英] Organize Android Realm data in lists

查看:188
本文介绍了整理列表中的Android Realm数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将当前的应用程序迁移到领域,并试图找出什么是将数据组织到范围的最佳方法.对于这个问题,我将专注于数据模型的Photo对象(但还有其他对象).

I'm looking into migrating our current app to realm and trying to figure out what's the best way to organize the data into ream. For this question I'll focus on the Photo object of my data model (but there're others).

我所有的数据对象都来自具有以下端点的API:getPopular()getNearbyPhotos(lat, lng)getUserPhotos(userId)getAlbumPhotos(albumId)getCollection(type).在大多数端点上,还有其他参数,例如sort=best/chronological.

All my data objects arrive from an API with endpoints such as: getPopular(), getNearbyPhotos(lat, lng), getUserPhotos(userId), getAlbumPhotos(albumId), getCollection(type). On most of those endpoints there're further parameters such as sort=best/chronological.

我们当前在项目上使用此非关系数据库,该数据库允许排序和检索列表仅使用listName的对象.我们将listName用作endpoit +参数的组合.

We're currently use this non-relational DB on the project which allows sorting and retrieving list of objects using only a listName. We use the listName as combination of the endpoit+parameters.

领域(作为关系数据库)使用标准查询,例如:

Realm (as a relational DB) uses standard queries, like:

realm.where(User.class)
     .contains("name", "Doe")
     .findAll();

当所有数据在本地可用时工作正常,但是这里的问题是这些查询的许多数据都保存在服务器端,并且从未发送给客户端.例如,Photo模型不包含位置,也不包含best排序的分数.

that works fine when all the data is available locally, but the problem here is that a lot of the data for those queries are kept on the server side and never sent to the client. For example the Photo model does not contain location nor a score for best sorting.

在我当前的测试项目中,我正在通过创建一个ListPhoto来解决该问题,该项目类似于我们当前项目将对象用作:

On my current test project, I'm working around that by creating a ListPhoto similar to what our current project uses object as:

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

,并使用实际的API端点和参数作为id.因此,我能够通过简单的查询找到这些对象

and using the actual API endpoint and parameters as id.So I'm able to find those objects with a simple query

ListPhoto listPhoto = realm
          .where(ListPhoto.class)
          .equalTo("id", id)
          .findFirst();

但是使用这种方法,我创建了一个抽象的额外层来简单地存储分组和排序元数据,对此我不满意,请在这里询问:

But with that approach I'm creating an extra layer of abstraction to simply store grouping and ordering metadata, which I'm not happy with and I ask here:

如何仅根据插入的顺序和组来查询领域中的数据,而无需使用我想到的这种肮脏的技巧?

how could I query data on realm based simply on the order and group that it was inserted without using this dirty hack I came up with?

对此的后续问题:查询包含在其他对象上的领域数据

推荐答案

但是,因为它是永远不会以可视化方式呈现给用户的数据,所以它不会发送给客户端(我想迁移到Realm,但最好这样做,而不必更改服务器端数据模型和endpoit行为)

But because it's data that is never visually presented to the user, it is not sent to the client (I would like to migrate to Realm, but would be better to do it without having to change server-side data models and endpoit behaviors)

那可惜但可以理解.

您所能做的就是为您的领域Photo对象提供字段,例如

What you can do is give your realm Photo objects the fields anyway, like

public class Photo extends RealmObject {
    // other fields

    private double latitude;
    private double longitude;
    private String score;
}

然后,像现在一样,使用API​​端点和参数查询服务器.

Then, you query the server with the API endpoint and parameters as you do now.

输入结果时,将它们存储在领域中,并为它们分配lat/long/score值.由于您刚刚在服务器查询中使用了它们,因此您现在可以使用它们.

When the results are in, you store them in realm and also assign them the values for lat/long/score. You have these available at this point because you just used them in the server query.

因此,如果您查询api/getNearbyPhotos(1, 2)?sort=best:
(伪代码)

So if you query for api/getNearbyPhotos(1, 2)?sort=best:
(pseudo code)

api.fetch("getNearbyPhotos(1, 2)?sort=best", new GetPhotoCompleteListener {
    @Override
    void onSuccess(List<Photo> photos) {
        // assuming gson or something has already parsed the server response into photo objects
        for (Photo p : photos) {
            p.setLatitude(1);
            p.setLongitude(2);
            p.setScore("best");
        }
        realm.copyToRealmOrUpdate(photos);
    }
}

现在,您在领域中有一堆照片对象,可以用与在服务器上相同的方式进行查询.

Now you have a bunch of photo objects in realm that you can query in the same way as you did on the server.

这篇关于整理列表中的Android Realm数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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