领域花费的时间太长,无法将对象复制到RealmObject [英] Realm taking too long to copy objects to RealmObject
问题描述
我正在使用领域将产品列表存储在我的Andorid应用程序中. 因此,我收到一个包含约3k个对象的产品列表. 而且我正在尝试像这样存储它们:
I'm using realm to store a list of Products in my Andorid App. So, I receive a produtc's list with about 3k objects. And I'm trying to store them like this:
@Override
public void saveAll(List<ProductsDomain> domainProducts) throws InstantiationException, IllegalAccessException {
Realm instance = getRealmInstance();
RealmList<ProdutcsRealm> realmProducts = new RealmList<ProdutcsRealm>();
try {
ProdutcsRealm realmProduct = getClasseEntidadePersistencia().newInstance();
for (ProductsDomain domainProduct : domainProducts) {
fromDomainToPersistence(domainProduct, realmProduct);
realmProducts.add(realmProduct);
}
instance.beginTransaction();
instance.copyToRealm(realmProducts);// taking to long, 3k items
instance.commitTransaction();
} catch (Exception e) {
e.printStackTrace();
instance.cancelTransaction();
return;
}
}
因此,该领域花费了太多时间,例如20分钟.任何人都有提高性能的想法吗?
So, the realm is taking too much time, something like 20 minutes. Anybody have any idea to get better performace?
已解决: 我发现了问题!我在所有迭代中都使用相同的ProductsRealm实例.尝试将多个引用的列表保存到同一对象时,似乎Realm不能很好地工作.
Solved: I've found the problem! I was using the same ProductsRealm instance for all iteration. Looks like that Realm dont work well when you try to save a list of multiple references to de same object.
推荐答案
如果可以在fromDomainToPersistence()
中将对象添加到Realm中,则会更快. saveAll()
方法可能类似于:
It will be faster if you can add the objects to your Realm in fromDomainToPersistence()
. The saveAll()
method could be something like:
public void saveAll(...) {
Realm instance = getRealmInstance();
try {
instance.beginTransaction();
for (ProductsDomain domainProduct : domainProducts) {
fromDomainToPersistence(instance, domainProduct);
}
instance.commitTransaction();
} catch (Exception e) {
// ...
}
}
和
public void fromDomainToPersistence(Realm r, DomainProduct domainProduct) {
ProductRealm realmProduct = r.createObject(ProductRealm.class);
// set the fields' values
}
这篇关于领域花费的时间太长,无法将对象复制到RealmObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!