Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realm, short look inside - Volodymyr Ischak

GDG Ternopil
September 20, 2016

Realm, short look inside - Volodymyr Ischak

Realm, short look inside - Volodymyr Ischak

GDG Ternopil

September 20, 2016
Tweet

More Decks by GDG Ternopil

Other Decks in Programming

Transcript

  1. -> SQL SELECT owner.name, dog.name, city.name FROM owner INNER JOIN

    dog ON owner.dog_id = dog.id INNER JOIN city ON owner.city_id = city.id WHERE owner.name = 'Frank'
  2. -> Java String query = "SELECT " + Owner.NAME +

    ", " + Dog.NAME + ", " + City.NAME + " FROM " + Owner.TABLE_NAME + " INNER JOIN " + Dog.TABLE_NAME + " ON " + Owner.DOG_ID + " = " + Dog.ID + " INNER JOIN " + City.TABLE_NAME + " ON " + Owner.CITY_ID + " = " + City.ID + " WHERE " + Owner.NAME = "'" + escape(queryName) + "'";
  3. //SKIP SLIDE JAVA RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 2).findAll(); puppies.size(); //

    => 0 realm.beginTransaction(); Dog dog = realm.createObject(Dog.class); dog.setAge(1); realm.commitTransaction(); puppies.size(); // => 1 // Change the dog from another query realm.beginTransaction(); Dog theDog = realm.where(Dog.class).equals("age", 1).findFirst(); theDog.setAge(3); realm.commitTransaction(); // Original dog is auto-updated dog.getAge(); // => 3 puppies.size(); // => 0
  4. //SKIP SLIDE SWIFT let puppies = realm.objects(Dog).filter("age < 2") puppies.count

    // => 0 because no dogs have been added to the Realm yet let myDog = Dog() myDog.name = “Rex” myDog.age = 1 try! realm.write { realm.add(myDog) } puppies.count // => 1 updated in real-time // Access the Dog in a separate query let puppy = realm.objects(Dog).filter("age == 1").first try! realm.write { puppy.age = 3 } // Original Dog object is auto-updated myDog.age // => 3 puppies.count // => 0
  5. Zero-copy in Realm User -name -title -book { -name -author

    } User -name -title -book User -name -title -book { -name -author } User -name -title -book User -name -title -book { -name -author } User -name -title -book { -name -author } ORM SQLite Realm Realm
  6. Zero-copy in Realm User jUser = new User(); // Standard

    Java POJO User proxyUser = realm.copyToRealm(jUser); // Convert POJO to Proxy User proxyUser = realm.createObject(User.class); // Proxy // Query results are lazy RealmResults< User > queryResults = realm.allObjects(User .class); queryResults.get(0) != queryResults.get(0); // Different Java objects queryResults.get(0).equals(queryResults.get(0)); // Same data
  7. Flatbuffers public long id() { int o = __offset(4); return

    o != 0 ? bb.getLong(o + bb_pos) : 0; } public String name() { int o = __offset(6); return o != 0 ? __string(o + bb_pos) : null; } ByteBuffer bb = ByteBuffer.wrap(bytes); MyObject obj = MyObject.getRootAsMyObject(bb); long id = obj.id(); String name = obj.name(); https://google.github.io/flatbuffers/
  8. UI Consistency Fragment 1 Fragment 2 • Loader • Content

    URI • ContentObserver • ContentProvider
  9. MVCC A B E F C D A B E

    F C D A’ B` F’ A` B` E F` C D A B F GARBAGE
  10. UI Consistency Fragment 2 Fragment 1 Fragment 2 Fragment 1

    new RealmChangeListener() { @Override public void onChange() { invalidateViews(); } }