Realm-java: How convert from List to RealmList<RelmInt>?

Created on 15 Sep 2017  路  10Comments  路  Source: realm/realm-java

My model:

public class Company extends RealmObject {
 @PrimaryKey
 private long id;
 private RealmList<RealmInt> favorIds;
 private String address;
 private String email;
}

public class RealmInt extends RealmObject {
    private int value;
}

In Realm in field favorIds I has the next values:

[10, 20, 30]

And I need to update favorIds with [10,20,30,40] in Realm:

My steps:

  1. Delete favorIds
  2. add update list

Here code:

  realm.executeTransaction(new Realm.Transaction() {

 @Override
 public void execute(Realm realm) {
         List<Integer> favorsIds = CompanyService.getFavoritesIds(selectedCompanyID);
         favorsIds.add(40);
         Company company = realm.createObject(Company.class);
         company.getFavors().deleteAllFromRealm();
         company.getFavors().addAll(favorsIds);
    }
});

But I get compile error when try to add List to RelmList:

Cannot resolve method "addAll(java.util.List<java.lang.Integer>)"

T-Help

Most helpful comment

@lmdic one day you should tell me how you ended up with such a terrible Realm schema, just to sate my curiosity :stuck_out_tongue:

Of course, as it is a RealmInt, you need to construct a managed RealmInt and add that into the favorIds managed list

         List<Integer> favorsIds = CompanyService.getFavoritesIds(selectedCompanyID);
         favorsIds.add(40);
         Company company = realm.createObject(Company.class);
         RealmList<RealmInt> favors = company.getFavors();
         favors.deleteAllFromRealm();
         for(Integer favorId : favorsIds) {
              RealmInt realmInt = realm.createObject(RealmInt.class);
              realmInt.value = favorId;
              favors.add(realmInt);
         }

All 10 comments

Why not

``` java
public class Contact extends RealmObject {
@PrimaryKey
private long id;
private RealmList favorites;

My model is

public class Company extends RealmObject {
 @PrimaryKey
 private long id;
 private RealmList<RealmInt> favorIds;
 private String address;
 private String email;
}

And I can't change it

@lmdic one day you should tell me how you ended up with such a terrible Realm schema, just to sate my curiosity :stuck_out_tongue:

Of course, as it is a RealmInt, you need to construct a managed RealmInt and add that into the favorIds managed list

         List<Integer> favorsIds = CompanyService.getFavoritesIds(selectedCompanyID);
         favorsIds.add(40);
         Company company = realm.createObject(Company.class);
         RealmList<RealmInt> favors = company.getFavors();
         favors.deleteAllFromRealm();
         for(Integer favorId : favorsIds) {
              RealmInt realmInt = realm.createObject(RealmInt.class);
              realmInt.value = favorId;
              favors.add(realmInt);
         }

I hope in future version of Realm you will create RealmObject for primitive types (integer, long, byte and so on). And as result user like me not need to use workaround like RealmInt.

@lmdic welp, RealmList<Integer> should be available in about 2 weeks

@lmdic I assume the above answered your question. Feel free to reopen if not.

Hey @cmelchior,
How could I get the type of RealmList on runtime in java?

Hey @Zhuinden,
Appreciate your quick reply, but found the solution on this,
Please refer below link worked like charm!

https://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list/1942680#1942680

Wow. That's cool. I didn't realize you can still access that through reflection. o-o

Was this page helpful?
0 / 5 - 0 ratings