在Realm中存储长对象列表的最有效方法是什么? [英] What is the most efficient way to store long list of Objects in Realm?

查看:118
本文介绍了在Realm中存储长对象列表的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将领域 Snappydb (这是我的回购,供那些希望了解基准测试的人使用.我想我的方法是错误的,因为与Sanppydb相比,Realm中存储到数据库的时间要花费超长的时间.

I'm trying to compare Realm with Snappydb (This is my repo for those who would like to have a look at benchmark). I guess my way is wrong, as store-to-db time takes super long time in Realm in compare with Sanppydb.

基准显示以下结果.从图中可以看到,Realm比Snappydb慢100-200倍.

Benchmark shows following result. As you can see in the image, Realm is around 100-200 times slower than Snappydb.

我正在做的是先创建10,000个对象,然后将它们存储到数据库中.因此,在我的代码中,我以这种方式存储了一个Booking对象(有一个for循环迭代10,000次):

What I'm doing is creating 10,000 objects first and then storing them into the db. So, in my code I store a Booking object in this way (there is a for loop that iterates 10,000 times):

public void storeBooking(final Booking booking) {
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                Booking realmBooking = realm.createObject(Booking.class);
                realmBooking.setId(booking.getId());
                realmBooking.setCode(booking.getCode());
                realmBooking.setFareLowerBound(booking.getFareLowerBound());
                realmBooking.setFareUpperBound(booking.getFareUpperBound());
                realmBooking.setPhoneNumber(booking.getPhoneNumber());
                realmBooking.setPickUpTime(booking.getPickUpTime());
                realmBooking.setRequestedTaxiTypeName(booking.getRequestedTaxiTypeName());
                realmBooking.setTaxiTypeId(booking.getTaxiTypeId());
            }
        });
    }

更新

这是用于存储预订对象的Snappydb方法.

Update

This is Snappydb method for storing Booking object.

public void storeBooking(final String key, final Booking booking) {
        try {
            mSnappyDb.put(key, booking);
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }

更新

使用insertOrUpdate()方法和一笔交易的新结果

Update

New results using insertOrUpdate() method and one transaction

推荐答案

您的原始解决方案可以在10000个事务中保存10000个对象,并为其创建10000个对象,因此,这几乎是最糟糕的方法.

Your original solution saves 10000 objects in 10000 transactions and creates 10000 objects for it, so that's pretty much the worst possible approach.

技术上,正确的方式应为:

public void storeBookings(final List<Booking> bookings) {
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.insertOrUpdate(bookings);
        }
    });
}


在大多数情况下,当保存的对象与原始对象不同时,我会这样做:


In most cases when the saved object is not the same as the original object, what I do is this:

public void storeBookings(final List<Booking> bookings) {
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RealmBook realmBook = new RealmBook();
            for(Booking booking : bookings) {
                realmBook = mapper.toRealm(booking, realmBook); // does not create new instance
                realm.insertOrUpdate(realmBook);
            }
        }
    });
}

此解决方案使用1个分离的对象来映射列表的内容.

This solution uses 1 detached object to map the content of the list.

这篇关于在Realm中存储长对象列表的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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