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

CBCouchbaseIncrementalStore

 CBCouchbaseIncrementalStore

Overview about NSIncrementalStore implementation that provides CouchDB, TouchDB and CouchbaseLite support for Core Data.

Christian Beer

August 21, 2013
Tweet

Other Decks in Programming

Transcript

  1. AGENDA • What is Core Data? • What is CouchDB

    / TouchDB / Couchbase? • How do they play together? • What is CBCouchbaseIncrementalStore? • Example
  2. • Persistence framework • Object graph management • Memory management

    (faulting) • Need more info? Watch Daniel Eggert explain it: http://www.uikonf.com/speakers/daniel_eggert.html WHAT IS CORE DATA? SQLite XML CouchDB ManagedObjectContext(s) PersistentStore(s) PSC Model
  3. WHAT IS COUCHDB? • NoSQL • Stores JSON • No

    SELECT, but views (map / reduce) • As TouchDB on mobile devices • no JavaScript but blocks for views • Replication / Syncing
  4. WHY COMBINE THEM? • Provides syncing (replication) • Object graph

    management for CouchDB • Core Data + flexible persistence • Ideal for early stage development
  5. SO WHAT IS CBCOUCHBASEINCREMENTALSTORE? • NSIncrementalStore implementation • Uses CouchCocoa

    or CouchbaseLite • Persistence to CouchDB, TouchDB or CouchbaseLite • CouchbaseLite not yet feature complete • Creates views for to-many relationships • Uses those in fetches to speed things up
  6. NSINCREMENTALSTORE? -loadMetadata: • Open the database and initialize all the

    views, etc. • Call -setMetadata:, eventually generate the Store-UUID • -executeRequest:withContext:error: • Central method for accessing the DB • NSSaveRequestType → insert / update / delete the data into DB • NSFetchRequestType → query the DB depending on resultType • Parses NSPredicate and tries to use the to-many views for relationships, ...
  7. NSINCREMENTALSTORE? • -newValuesForObjectWithID:withContext:error: • Load document from DB and fill

    the properties into NSIncrementalStoreNode • -newValueForRelationship:forObjectWithID:withContext:error: • to-many: query the to-many-view and return NSManagedObjectIDs • to-one: load the document and returns either the NSManagedObjectID or NSNull • -obtainPermanentIDsForObjects:error: • generates a reference-object from a UUID and the entity name and • calls -newObjectIDForEntity:referenceObject: • ... plus many helper methods
  8. WHAT‘S THE STATE OF IT? • Works like a charm

    • Used by a small test group using iVocabulary • Users could collaborate via filtered replications • Slow when re-creating views • Sometimes 10+ seconds when fetching data • Slow in some fetches • In worst case needs to iterate all documents
  9. MISSING FEATURES • CouchbaseLite support needs improvements • Change tracking

    could be improved • NSPredicate not fully supported, yet (but for 95%) • Documentation / Samples
  10. SO, HOW DO I USE IT? • Add dependencies: •

    libCouchbaseIncrementalStore.a • for TouchDB: libCouchCocoa.a, libTouchDB.a • for CouchDB: libCouchCocoa.a • for CouchbaseLite: libCouchbaseLite.a • + some other dependencies
  11. SO, HOW DO I USE IT? • Modify the model

    before first use (adds revision ID): [CBCouchCocoaIncrementalStore updateManagedObjectModel:mom]; • Change store type to: [CBCouchCocoaIncrementalStore type] • Change store URL to: storeURL = [directoryURL URLByAppendingPathComponent: @"<foldername>.touchdb/<database-name>"];
  12. REPLICATION • As easy as: NSURL *remoteURL = [NSURL URLWithString:@"http://<server>:5984/<database>"];

    [store replicateWithURL:remoteURL exclusively:YES]; • But: the persistent store doesn‘t know about the contexts • Your code needs to tell the contexts about changes • handle kCBISObjectHasBeenChangedInStoreNotification • create a NSManagedObjectContextDidSaveNotification for merging
  13. LINKS • Demo Sourcecode (original, by M. Zarra): http://pragprog.com/titles/mzcd2/source_code (doesn‘t

    work out of the box, though) • CBCouchbaseIncrementalStore: https://github.com/chbeer/CBCouchbaseIncrementalStore • CouchDB: http://couchdb.apache.org • TouchDB: http://touchdb.org • CouchCocoa: https://github.com/couchbaselabs/CouchCocoa • NSHipster on NSIncrementalStore: http://nshipster.com/nsincrementalstore/