如何在Android Room中存储对象? [英] How to store objects in Android Room?

查看:322
本文介绍了如何在Android Room中存储对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我不了解两件事:带有对象的对象和带有对象列表的对象

Basically, there are two things I don't understand: objects with objects and objects with lists of objects

说我收到来自服务器的对象列表.他们每个人都像这样:

Say I receive a list of objects from the server. Each of them looks like this:

@Entity
public class BigObject {
    @PrimaryKey
    private int id;
    private User user;
    private List<SmallObject> smallObjects;
}

以这两个对象作为字段:

with these two objects as fields:

@Entity
public class User {
    @PrimaryKey
    private int id;
    private String name;
    @TypeConverters(GenderConverter.class)
    public MyEnums.Gender gender;
}

@Entity
public class SmallObject {
    @PrimaryKey (autoGenerate = true)
    private int id;
    private String smallValue;
}

它们比这还要复杂,所以我不能按照Room的建议使用@TypeConverters:

They are more complicated than this, so I can't use @TypeConverters as Room suggests:

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.

如何在Room中存储此数据结构?

How do I store this data structure in Room?

推荐答案

我认为回答这个问题的最好方法是存储结构中的breif概述...

I think the best way to answer this is a breif overview in storing structures...

列表

Room不支持存储嵌套在POJO内的列表.推荐的存储列表的方法是使用外键方法.将对象列表存储在一个单独的表(在本例中为smallObjects表)中,并为其相关父对象(在本例中为"big_object_id")使用外键.它应该看起来像这样...

Room does not support storing lists that are nested inside of a POJO. The recommended way to store lists is to use the foreign key approach. Store the List of objects in a seperate table (in this case a smallObjects table) with a foreign key to their related parent object (in this case "big_object_id"). It should look something like this...

@Entity
public class BigObject {
    @PrimaryKey
    private int id;
    private User user;
    @Ignore
    private List<SmallObject> smallObjects;
}

@Entity(foreignKeys = {
            @ForeignKey(
                entity = BigObject.class,
                parentColumns = "id",
                childColumns = "big_object_fk"
            )})
public class SmallObject {
    @PrimaryKey (autoGenerate = true)
    private int id;
    private String smallValue;
    @ColumnInfo(name = "big_object_fk")
    private int bigObjectIdFk
}

请注意,我们已在 List< SmallObject> 中添加了 @Ignore 注释,因为我们希望在Room持续期间忽略该字段(因为不支持列表).现在已经存在,因此当我们从数据库中请求相关的小对象列表时,我们仍然可以将它们存储在POJO中.

Note that we have added the @Ignore annotaiton to List<SmallObject> as we want to ignore the field during Room persistance (as lists are not supported). It now exists so that when we request our list of related small objects from the DB we can still store them in the POJO.

据我所知,这意味着您要进行两个查询.

To my knowledge this will mean you are making two queries.

BigObject b = db.BigObjectDao.findById(bOId);
List<SmallObject> s = db.smallObjectDao.findAllSOforBO(bOId);
b.setsmallObjects(s);

看来,这是 类型转换器

这些情况适用于您具有可以在不丢失信息的情况下进行展平并存储在单个列中的复杂数据结构的情况.Date对象就是一个很好的例子.Date对象很复杂,并且拥有很多值,因此将其存储在数据库中非常棘手.我们使用类型转换器来提取日期对象的毫秒表示形式并将其存储.然后,我们在出路时将毫秒转换为日期对象,从而保持数据完整.

These are for cases where you have a complex data structure that can be flattend without losing information, and stored in a single column. A good example of this is the Date object. A Date object is complex and holds a lot of values, so storing it in the database is tricky. We use a type converter to extract the milli representation of a date object and store that. We then convert the millis to a date object on the way out thus keeping our data intact.

嵌入式

当您要获取父POJO中所有嵌套POJO的字段并将其展平以存储在一个表中时,使用此方法.一个例子:

This is used when you want to take the fields of all nested POJOs in your parent POJO and flatten them out to store in one table. an example :

- name
- age
- location
    - x 
    - y
- DOB

..当嵌入时,此结构将存储为:

..when embedded this structure would be stored in the database as :

- name 
- age 
- location_x
- location_y
- DOB

从某种意义上说,存在Embedded可以节省您为每个包含基本类型字段(例如String,int,float等)的嵌套对象创建类型转换器的时间.

In a sense Embedded exists to save you time creating type converters for every nested object that contains primary type fields like String, int, float, etc...

这篇关于如何在Android Room中存储对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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