25
BEYOND CORE DATA: NOSQL FOR MOBILE Sam Ritchie

Beyond Core Data - YOW! Conferences and Workshopsyowconference.com.au/slides/yowconnected2014/Ritchie-BeyondCore… · BEYOND CORE DATA: NOSQL FOR MOBILE Sam Ritchie. APP DATA STORAGE

  • Upload
    lamkiet

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

BEYOND CORE DATA: NOSQL FOR MOBILE

Sam Ritchie

APP DATA STORAGEiOS Data Storage

0%

15%

30%

45%

60%

Core Data SQLite FMDB Magical Record

DATABASESDATABASES

Logical model

Arbitrary queries

Concurrency

Tool support

Existing skills

RELATIONAL

Replication

Full Text indexing

Spatial indexing

Schema upgrades

Object-Relational impedance

Order

Order

Order

Customer

Customer

Customer

Order Line

Order Line

Order Line

Product

Product

Product

Order

Customer

Order Line

Product

Order Line

Product

Order Line

Product

WHAT ARE THE OPTIONS?

The File System

YapDatabase

CloudKit

BENCHMARKS

{ title : …, body : …, createdDate : …, ownerId : “users/1234”, ownerDisplayName : “Sam”, … answers : [ { score : …, body : …, ownerId : “users/4567”, ownerDisplayName : “Jon Skeet”, comments : [] }], comments : [ { score : …, text : …, ownerId : “users/1234”, ownerDisplayName : “Sam” }], tags : [ “objective-c”, “iOS” ], duplicates : [ “questions/456” ], related : [] }

questions/123 users/1234{ displayName : “Sam” reputation : 1337, profileImageURL : …, websiteURL : …, age : …, location : …, creationDate : …, lastAccessDate : … }

Fetch Group By Full Text Search LOC

Core Data Realm CouchBase Lite YapDatabase LevelDB

Awesome

Crap

LEAKY ABSTRACTIONS

COST OF ABSTRACTION

CBLQuery *query = [[self.database viewNamed:@"tags"] createQuery]; query.groupLevel = 1; for (CBLQueryRow *row in [query run:NULL]) NSLog(@"%@ (%@)", row.key, row.value);

Couchbase Lite

__block NSString *currentTag = nil; __block int count = 0; [database enumerateKeysWithPrefix:@"i/tags/" block:^(NSString *key, BOOL *stop) { NSString *tag = [[key stringByDeletingLastPathComponent] stringByReplacingOccurrencesOfString:@"i/tags/" withString:@""]; if (!currentTag || ![currentTag isEqualToString:tag]) { NSLog(@"%@ (%d)", currentTag, count); count = 0; currentTag = tag; } count++; }]; NSLog(@"%@ (%d)", currentTag, count);

LevelDB

COST OF ABSTRACTION

select name, count(1) from Tags group by name

NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Tag"]; NSEntityDescription* entity = [NSEntityDescription entityForName:@"Tag" inManagedObjectContext:self.context]; NSAttributeDescription* nameDesc = [entity.attributesByName objectForKey:@"name"]; NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"name"]; NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName: @"count"]; [expressionDescription setExpression: countExpression]; [expressionDescription setExpressionResultType: NSInteger32AttributeType]; [fetch setPropertiesToFetch:[NSArray arrayWithObjects:nameDesc, expressionDescription, nil]]; [fetch setPropertiesToGroupBy:[NSArray arrayWithObject:nameDesc]]; [fetch setResultType:NSDictionaryResultType]; NSError* error = nil; NSArray *results = [self.context executeFetchRequest:fetch error:&error]; for (NSDictionary *result in results) NSLog(@"%@ (%@)", result[@"name"], result[@"count"]);

Core Data

REPLICATION

Order

Order

Order

Customer

Customer

Customer

Order Line

Order Line

Order Line

Product

Product

Product

Order

Order

Order

Customer

Customer

Customer

Order Line

Order Line

Order Line

Product

Product

Product

Order Line

OrderOrder

Customer

Customer

Product

Order +

Order

Order Line

Order Line

Order

Order Line

Product

Order Line

Product

Order Line

Product

Customer

Order

Customer

Order Line

Product

Order Line

Product

Order Line

ProductProduct

Customer

!

LIVE CODING DEMO

THANKS!

[email protected]

http://codesplice.com.au

@FakeSamRitchie