Skip to content

Instantly share code, notes, and snippets.

@alekseichuk
Last active August 29, 2015 14:25
Show Gist options
  • Save alekseichuk/36d5ac0c34d3e315a0ca to your computer and use it in GitHub Desktop.
Save alekseichuk/36d5ac0c34d3e315a0ca to your computer and use it in GitHub Desktop.
RealmList Bug
/*Realm incorrect nested managed RealmList BUG*/
public class ProductOption extends RealmObject {
@PrimaryKey private String uniqueId;
private long id;
private String title;
private int format;
private Product product;
private RealmList<ProductSubOption> suboptions;
//Getters ans detters are here
}
public class ProductSubOption extends RealmObject {
@PrimaryKey private String uniqueId;
private long id;
private String name;
private double price;
private String description;
private ProductOption option;
//Getters ans detters are here
}
/*Way of adding items to Realm is:*/
for (ProductOptionModel opt : p.getOptions()) {
ProductOption productOption = new ProductOption();
//call setters
mRealm.copyToRealmOrUpdate(productOption)
for (ProductSubOptionModel subOpt : opt.getSubOptions()) {
ProductSubOption productSubOption = new ProductSubOption();
//call setters
mRealm.copyToRealmOrUpdate(productSubOption);
}
}
/*Way of fetching options and suboptions*/
RealmResults<ProductOption> options = mRealm
.where(ProductOption.class)
.equalTo("product.id", productId)
.findAll();
int i = ...; //any valid number here
options.get(i).getSubOptions(); // !!! ISSUE HERE !!! instead of getting e.g. 6 elements just 5 returned
/*If to query ProductSubOption separately everything OK*/
int someOptionsId = ...;
RealmResults<ProductSubOption> subOptions = mRealm
.where(ProductSubOption.class)
.equalTo("option.id", someOptionsId)
.findAll();
/*In this case result is correct. All items returned*/
subOptions.size() == 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment