Skip to content

Instantly share code, notes, and snippets.

@kevinvanmierlo
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save kevinvanmierlo/3e7f37b25fcd3ccf0223 to your computer and use it in GitHub Desktop.

Select an option

Save kevinvanmierlo/3e7f37b25fcd3ccf0223 to your computer and use it in GitHub Desktop.

Revisions

  1. kevinvanmierlo revised this gist Apr 15, 2015. 6 changed files with 0 additions and 3 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Person:
    1 change: 0 additions & 1 deletion gistfile3.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Group:
    1 change: 0 additions & 1 deletion gistfile5.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Testing code:
  2. kevinvanmierlo created this gist Apr 15, 2015.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Person:
    27 changes: 27 additions & 0 deletions gistfile2.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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;
    }
    }
    1 change: 1 addition & 0 deletions gistfile3.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Group:
    14 changes: 14 additions & 0 deletions gistfile4.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    public class Group extends RealmObject
    {
    private RealmList<Person> persons;

    public RealmList<Person> getPersons()
    {
    return persons;
    }

    public void setPersons(RealmList<Person> persons)
    {
    this.persons = persons;
    }
    }
    1 change: 1 addition & 0 deletions gistfile5.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Testing code:
    45 changes: 45 additions & 0 deletions gistfile6.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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());
    }