您如何将copyToRealmOrUpdate用于列表?

时间:2018-11-12 08:36:01

标签: android realm

这是我的Android应用程序中的一些代码:

public class Offer extends RealmObject {
    @PrimaryKey
    private long id;

}

在我的服务班级:

  RealmList<Offer> currentLocalMerchantOfferList = currentLocalMerchant.getOffers();
   RealmList<Offer> findIncomingMerchantOfferList = findIncomingMerchant.getOffers();
                                if (!EqualsUtil.areEqual(currentLocalMerchantOfferList, findIncomingMerchantOfferList)) {
                                    currentLocalMerchant.setOffers(findIncomingMerchantOfferList == null ? null : realm.copyToRealmOrUpdate(findIncomingMerchantOfferList));
                                }

我收到compile错误:

error: incompatible types: bad type in conditional expression
                                currentLocalMerchant.setOffers(findIncomingMerchantOfferList == null ? null : realm.copyToRealmOrUpdate(findIncomingMerchantOfferList));

我正确使用copyToRealmOrUpdate吗?如果没有,您如何正确使用它?

2 个答案:

答案 0 :(得分:1)

realm.copyToRealmOrUpdate(findIncomingMerchantOfferList)返回List<T>而不是RealmList<T>

RealmList将many-relationship表示为另一种对象类型。因此,这些随机插入的对象不是多关系,因此它们不是RealmList。

实际上,它们在内部以ArrayList的形式返回。

更改代码以适应代码的方法是:

currentLocalMerchant.getOffers().clear();
currentLocalMerchant.getOffers().addAll(realm.copyToRealmOrUpdate(findIncomingMerchantOfferList));

答案 1 :(得分:-1)

我认为您不需要使用copyToRealmOrUpdate方法来设置值。只需设置您拥有的值

 if (!EqualsUtil.areEqual(currentLocalMerchantOfferList, findIncomingMerchantOfferList)) {
                                        currentLocalMerchant.setOffers(findIncomingMerchantOfferList)
                                    }

如果将findIncomingMerchant.getOffers()返回给您持久性数据,则它将非常有效。