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

Live Objects with Realm

Live Objects with Realm

Value Objects are preached by followers of the functional reactive programming paradigm in the mobile development community. They are neat, but Live Objects have their own advantages as well. They always reflect the latest modifications across threads and processes. This talk reminds of those and explains how they give a whole new experience when working with persistency as given by the Realm database. Beside a general introduction in both concepts and their differentiation, you can expect a technical deep dive in how the Realm Cocoa bindings are implementing them.

Marius Rackwitz

May 27, 2016
Tweet

More Decks by Marius Rackwitz

Other Decks in Programming

Transcript

  1. 2

  2. 3

  3. Objective-C @interface InventoryItem : NSObject @property (nonatomic, copy) NSString *name;

    @property (nonatomic, assign) NSInteger quantity; @property (nonatomic, strong) Category *category; - (instancetype)itemWithName:(NSString *)name quantity:(NSInteger)quantity category:(Category *)category; @end 10
  4. Java public final class InventoryItem { private final String name;

    private final Int quantity; private final Category category; public InventoryItem(String name, Int quantity, Category category) { this.name = name; this.quantity = quantity; this.category = category; } public String getName() { return name; } public Int getQuantity() { return quantity; } public Category getCategory() { return category; } } 12
  5. How to Interact with These? e.g. Retrieve an InventoryItem from

    a database, modify the quantity, persist the change in the database. 13
  6. Retrieve Cursor c = db.query("inventory_items", { "name", "quantity", "categoryId" },

    "name = ?", { "Drilling machine" }, null, null, null, 1); c.moveToFirst(); String name = c.getString(c.getColumnIndexOrThrow("name")); Int quantity = c.getInt(c.getColumnIndexOrThrow("quantity")); Long categoryId = c.getLong(c.getColumnIndexOrThrow("categoryId")); Category category = getCategoryById(categoryId); InventoryItem item = InventoryItem(name, quantity, category); 14
  7. Persist // New value for one column ContentValues values =

    new ContentValues(); values.put("name", modifiedItem.name); values.put("categoryId", modifiedItem.category.id); values.put("quantity", modifiedItem.quantity); int count = db.update( "inventory_items", values, "name = ?", { item.name }); 16
  8. Live Objects 4 Fundamentally Different 4 Always Represent the Latest

    State 4 Navigatable 4 But Not Necessarily Thread-Safe 20
  9. Example import RealmSwift class InventoryItem : Object { dynamic var

    name: String dynamic var quantity: Int = 1 dynamic var category: Category? } 21
  10. Retrieve, Persist, Modify let results = realm.objects(InventoryItem) .filter("name = %@",

    "Drilling machine") let obj = results.first! realm.write { obj.quantity += 1 } 22
  11. ! Objects store a reference to their row under the

    hood. They always carry identity. 24
  12. !→ " !"#→ ✅ Modifications are only allowed within write

    transactions and directly occur on the database. 26
  13. 30

  14. 31

  15. 32

  16. Advantages of Realm's Live Objects Concept 4 Single source of

    truth for your model schema 4 No overhead of mapping data around 4 Change notifications for free 40