Skip to content

Instantly share code, notes, and snippets.

@kevinvanmierlo
Last active August 29, 2015 14:19
Show Gist options
  • Save kevinvanmierlo/3e7f37b25fcd3ccf0223 to your computer and use it in GitHub Desktop.
Save kevinvanmierlo/3e7f37b25fcd3ccf0223 to your computer and use it in GitHub Desktop.
RealmList move - unexpected results
public class Group extends RealmObject
{
private RealmList<Person> persons;
public RealmList<Person> getPersons()
{
return persons;
}
public void setPersons(RealmList<Person> persons)
{
this.persons = persons;
}
}
public class Person extends RealmObject
{
@PrimaryKey
private int id;
private String name;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
Group group;
if(realm.allObjects(Group.class).size() == 0)
{
group = realm.createObject(Group.class);
}else
{
group = realm.allObjects(Group.class).first();
}
realm.commitTransaction();
for(int i = 1; i <= 10; i++)
{
Person person = new Person();
person.setId(i);
person.setName("Name " + i);
realm.beginTransaction();
Person databasePerson = realm.copyToRealmOrUpdate(person);
realm.commitTransaction();
group.getPersons().add(databasePerson);
}
for(int i = 0; i < 10; i++)
{
System.out.println("first list: " + group.getPersons().get(i).getName());
}
group.getPersons().move(1, 0);
// Gets handled correctly
for(int i = 0; i < 10; i++)
{
System.out.println("second list - move up: " + group.getPersons().get(i).getName());
}
group.getPersons().move(0, 1);
// Same as second list, but should be the same as the first list
for(int i = 0; i < 10; i++)
{
System.out.println("third list - move down: " + group.getPersons().get(i).getName());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment