领域:按键加入两个RealmObject

时间:2015-12-28 14:39:55

标签: android realm

我有两个public class Category extends RealmObject { @PrimaryKey private int categoryId; private String name; public String getName() { return name; } public int getCategoryId() { return categoryId; } public void setName(String name) { this.name = name; } public void setCategoryId(int categoryId) { this.categoryId = categoryId; } } 模型。

分类

public class Event extends RealmObject {

    @PrimaryKey
    private int eventId;
    private String title;
    private Category category;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}

事件

  [
        {
            "categoryId": 1,
            "name": "Category 1"
        },  
        {
            "categoryId": 2,
            "name": "Category 2"
        }
        ...
    ]

我使用两个不同的请求从Web服务获取数据:

类别回复

[
    {
        "eventId": 0,
        "title": "Event 0",
        "category": {
            "categoryId": 1,
            "name": null            
          }
    },
    {
        "eventId": 1,
        "title": "Event 1",
        "category": {
            "categoryId": 2,
            "name": null            
          }
    }
]

活动回复

categories

因此,在映射和保存数据后,我得到所有realm.where(Category.class).findAll();

categories

返回一些//0 categoryId = 0 name = "Category 0" //1 categoryId = 1 name = "Category 1" //2 categoryId = 2 name = "Category 2" //3 categoryId = 3 name = "Category 3"

events

获取所有realm.where(Event.class).findAll();

events

返回一些//0 Event = [{eventId:0},{title:Event 0},{category:[{categoryId:1},{name:null}]}] eventId = 0 title = "Event 0" category = { categoryId = 1 name = null } //1 Event = [{eventId:1},{title:Event 1},{category:[{categoryId:2},{name:null}]}] eventId = 1 title = "Event 1" category = { categoryId = 2 name = null }

Name

Category中的字段category为空。那么,我怎样才能获得其他Realm字段(名称)?我认为sql join会返回完整的类别对象,但不会。我需要一些喜欢index的人吗?可以不改变模型结构吗?

0 个答案:

没有答案