我需要解决如何将数据放入这样的领域数据库:
我有一个名为获取代码的对象;
我在一个名为offer的对象中有一个获取代码的真实列表;
我分别下载获得的代码,并根据它们的报价ID将它们分配给每个对象的列表。问题是我无法添加它们,因为当我检查大小时,它总是0。
代码如下:
ObtainedCodes codes = response.body();
for (ObtainedCode c : codes.getObtainedCodes()) {
Offer offer = RealmController.with(SplashActivity.this).getOffer(c.getOffer_id());
if (offer != null) {
Log.d("Size", "Offer not null");
realm1.beginTransaction();
RealmList<ObtainedCode> list = offer.getObtained_codes();
if (!list) { // if the 'list' is managed, all items in it is also managed
RealmList<ObtainedCode> managedImageList = new RealmList<>();
for (ObtainedCode item : list) {
if (item) {
managedImageList.add(item);
} else {
managedImageList.add(realm1.copyToRealm(item));
}
}
list = managedImageList;
}
offer.setObtained_codes(obtainedCodes);
Log.d("Size", String.valueOf(offer.getObtained_codes().size()));
realm1.copyToRealmOrUpdate(offer);
realm1.commitTransaction();
}
offer = RealmController.with(SplashActivity.this).getOffer(c.getOffer_id());
Log.d("Size", String.valueOf(offer.getObtained_codes().size()));
}
最佳答案
1.)关于infohive的ravi tamada教程一团糟,请参考my remake of that example instead。
如果你开始使用0.82.1是因为Ravi Tamada声称4岁的版本是“稳定的”,那么我知道它不是。改用1.2.0(或3.4.1的最新版本)
如果看到RealmController.with()
,则运行,因为它忽略线程限制。当你试图从后台线程访问它时,它会崩溃。
在后台线程中,您需要
@Override
public void run() {
try(Realm realm = Realm.getDefaultInstance()) {
repository.whatever(realm); // pass Realm instance to database methods
} // auto-close
// end of thread
}
2.)您正在ui线程上执行写操作,这是不好的,从ui线程应该使用
realm.executeTransactionAsync()
,但在您的情况下,您应该实际使用Ęxecutors.newSingleThreadedPool()
在后台线程上执行改型调用,并使用call.execute()
而不是call.enqueue()
调用它。3.)您应该在后台线程上写入realm,而在ui线程上,您应该使用realmchangelister来侦听写入。
4.)您的代码无法工作,因为您正在将非托管列表设置为托管realmobject。
您应该修改RealMube中的现有RealMLIST,并只向其添加托管对象。
Executor executor = Executors.newSingleThreadExecutor(); // field variable
// ...
void someMethod() {
executor.execute(new Runnable() {
@Override
public void run() {
Response<ObtainedCodes> response = retrofitService.getObtainedCodes().execute(); // run on current thread
ObtainedCodes codes = response.body();
if(codes == null) return;
try(Realm r = Realm.getDefaultInstance()) {
r.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for(ObtainedCode obtainedCode : codes.getObtainedCodes()) {
Offer offer = realmRepository.getOffer(realm, obtainedCode.getOfferId());
if(offer == null) {
offer = realm.createObject(Offer.class, obtainedCode.getOfferId());
// map properties to offer if possible
}
RealmList<ObtainedCode> offerCodes = offer.getObtainedCodes();
ObtainedCode managedObtainedCode = realm.where(ObtainedCode.class).equalTo("obtainedCodeId", obtainedCode.getId()).findFirst();
if(managedObtainedCode == null) {
managedObtainedCode = realm.createObject(ObtainedCode.class, obtainedCode.getId());
// map properties from obtained code to managed obtained code
}
if(!offerCodes.contains(managedObtainedCode)) {
offerCodes.add(managedObtainedCode);
}
}
}
});
}
}
});
}
关于android - 域添加到RealmList的项,该项已添加到RealmObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42447505/