Skip to content

Instantly share code, notes, and snippets.

@Gryzor
Last active August 29, 2015 14:21
Show Gist options
  • Save Gryzor/89d4d92b0342c49ab78e to your computer and use it in GitHub Desktop.
Save Gryzor/89d4d92b0342c49ab78e to your computer and use it in GitHub Desktop.
// getter/setter ommited
class A extends RealmObject {
@PrimaryKey
private String name;
}
class B extends RealmObject {
@PrimaryKey
private String name;
private A a;
}
void do() {
A a = new A();
a.setName("a");
B b = new B();
b.setName("b");
b.setOne(a);
B c = new B();
c.setName("c");
c.setOne(a);
realm = Realm.getInstance(…);
realm.beginTransaction();
realm.copyToRealm(b);
realm.copyToRealm(c);
realm.commitTransaction();
realm.close();
}
// if we use copyToRealmOrUpdate(), doesn't this cause the 'c' object to UPDATE the existing 'a' (inserted by 'b')?
// If we don't, we get a PK violation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment