使用ObjectBox储存复杂对象

本文介绍了如何使用ObjectBox来存储具有复杂关系的数据,如一对一和一对多关系。详细阐述了定义关系,如MovieListContent与MovieListSubjectManager的一对一关系,以及Subject与Rating、Image的一对一关系。接着,文章讨论了设计实体类的层次结构,并分享了存储和从ObjectBox检索对象的实践经验。作者提到理解这些概念花费了一天时间,尽管过程中遇到挑战,但仍然认为ObjectBox是一个非常好用的库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

使用objectbox可以储存一些具有复杂关系的数据,比如一对多关系,比如一对多关系和多对多关系.

定义关系

比如我要储存这样关系的数据 image MovieListContent对subjectmanager是一个一对一关系

subjectmanager对movielistsubject是一对多的关系

每一个subject对rating和image都是一对一的关系

设计实体类

层次由高到低是这样设计实体的

MovieListContent

@Entity
public class MovieListContent {

    @Id
    @Expose
    private long databaseId;

    // 搜索结果的开始条目
    private String start;
    // 本此搜索的数目
    private String count;
    // 搜索的标题
    private String title;
    // 搜索的条目总数
    private String total;
    // 电影的条目
    @Transient
    private ArrayList<MovieListSubject> subjects;

    @Expose
    private ToOne<MovieListSubjectManager> movieListSubjectManagerToOne;
    // 省略getter和setter
}

MovieListSubjectManager

@Entity
public class MovieListSubjectManager {

    @Id
    private long databaseId;

    private ToMany<MovieListSubject> movieListSubjectToMany;
}

Rating

@Entity
public class Rating {

    @Id
    @Expose
    private long databaseId;

    private String max;
    private String average;
    private String stars;
    private String min;
}

Image

@Entity
public class Image {

    @Id
    @Expose
    private long databaseId;

    private String small;

    private String large;

    private String medium;
}

储存

        final BoxStore boxStore = ((MyApplication) getApplication()).getBoxStore();
        final Box<MovieListContent> movieListContentBox = boxStore.boxFor(MovieListContent.class);
        final Box<MovieListSubject> movieListSubjectBox = boxStore.boxFor(MovieListSubject.class);
        final Box<Rating> ratingBox = boxStore.boxFor(Rating.class);
        final Box<MovieListSubjectManager> movieListSubjectManagerBox = boxStore.boxFor(MovieListSubjectManager.class);
        Box<Image> imageBox = boxStore.boxFor(Image.class);
        movieListContentBox.removeAll();
        movieListSubjectBox.removeAll();
        ratingBox.removeAll();
        imageBox.removeAll();
        movieListSubjectManagerBox.removeAll();
        DoubanApiService service = ApiServiceSington.getService();
        Observable<MovieListContent> observable = service.getMovieSearceContent("高圆圆", "0", "10");
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MovieListContent>() {
                    @Override
                    public void accept(MovieListContent movieListContent) throws Exception {
                        List<MovieListSubject> movieListSubjects = movieListContent.getSubjects();
                        MovieListSubjectManager movieListSubjectManager = new MovieListSubjectManager();
                        for (MovieListSubject subject : movieListSubjects) {
                            subject.getImageToOne().setTarget(subject.getImage());
                            subject.getRatingToOne().setTarget(subject.getRating());
                        }
                        movieListSubjectManager.getMovieListSubjectToMany().addAll(movieListSubjects);
                        movieListContent.getMovieListSubjectManagerToOne().setTarget(movieListSubjectManager);
                        movieListContentBox.put(movieListContent);
                    }
                });

从objectbox中获取对象

BoxStore boxStore = ((MyApplication) getApplication()).getBoxStore();
        Box<MovieListContent> movieListContentBox = boxStore.boxFor(MovieListContent.class);
        MovieListContent movieListContent = movieListContentBox.get(20);
        Log.d("tinuv_movieListContent", movieListContent.getTitle());
        MovieListSubjectManager manager = movieListContent.getMovieListSubjectManagerToOne().getTarget();
        for (int i = 0; i < 10; i++) {
            MovieListSubject movieListSubject = manager.getMovieListSubjectToMany().get(i);
            Log.d("tinuv_image", movieListSubject.getImageToOne().getTarget().getLarge());
            Log.d("tinuv_rating", movieListSubject.getRatingToOne().getTarget().getAverage());
            Log.d("tinuv_movielistsubject", movieListSubject.getTitle());
        }

总结

搞懂这些东西其实花了我很长时间(大概一天),因为英语不好,很多地方看文档看不懂,只能一遍一遍尝试,在尝试的过程中程序崩溃了很多次,我也崩溃了很多次,但是还是要感谢这个库的开发者,因为真的很好用也很有用.

参考

ObjectBox官方网站:https://docs.objectbox.io/

??正文结束??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值