Reputation:
When I install my app for first time on my phone, the app crashes because of the Realm error: This method is only available in managed mode
which I really don't understand much of
This happends when I try to get a sorted list of ListItem
in the class Shoplist
public class Shoplist extends RealmObject implements Serializable {
private RealmList<ListItem> itemList; //ListItem extends `RealmObject`
@Ignore
private Realm realm;
@PrimaryKey
private long id;
public Shoplist() {
realm = Realm.getDefaultInstance();
}
public RealmList<ListItem> getItemList() {
return itemList;
}
public List<ListItem> getItems(String listOrder) {
RealmResults<ListItem> realmResults;
switch (listOrder) {
case PrefActivity.ASCENDING:
realmResults = getItemList().where().findAll(); // this crashes!
break;
case PrefActivity.DESCENDING:
realmResults = getItemList().where().findAllSorted(ListItem.TIME_STAMP, Sort.DESCENDING);
break;
}
return realmResults;
}
}
A Shoplist object is created like this in a class called RealmService
public Shoplist createShoplist(String title) {
realm = Realm.getDefaultInstance();
realm.beginTransaction();
Shoplist shoplist = new Shoplist(new RealmList<ListItem>(), title);
realm.copyToRealm(shoplist);
realm.commitTransaction();
return shoplist;
}
STATS:
Realm Gradle Plugin: 2.3.1
Gradle Plugin: 2.2.3
Compile version: 25
Android Studio version: 2.2.3
Test phone: Samsung Galaxy S7
Upvotes: 1
Views: 2688
Reputation: 5361
You need work with managed object. Your error happen when Shoplist
object created with new
operator - it's not managed object.
You need to wrap instance of Shoplist
to Realm:
public Shoplist createShoplist(String title) {
realm = Realm.getDefaultInstance();
realm.beginTransaction();
Shoplist shoplist = realm.createObject(Shoplist.class)
shoplist.setTitle(title);
realm.copyToRealm(shoplist);
realm.commitTransaction();
return shoplist;
}
Upvotes: 1