Skip to content

CBLIncrementalStore

Sören Havemester edited this page Mar 27, 2017 · 8 revisions

CBLIncrementalStore (Draft)

1. What is CBLIncrementalStore

NSIncrementalStore was introduced in iOS5 as an abstract subclass of NSPersistentStore to allow us to create a persistent store for Core Data to incrementally load & save data and query data.

CBLIncrementalStore is a subclass of the NSIncrementalStore to allow Core Data to use Couchbase Lite database as its persistent storage instead of the default SQLite database. The main benefit of opting-in to use Couchbase Lite is to get Core Data Synced with the remote Couchbase Server database.

There is a good write-up about the NSIncrementStore here if you are interested in.

2. How to use CBLIncrementalStore?

Where is CBLIncremetalStore?

CBLIncrementalStore is packaged in Extras folder of the couchbase-lite-ios binary zip file. To use CBLIncrementalStore, you will need to copy and put the CBLIncrementalStore.h and CBLIncrementalStore.m directly into your application project.

How to create NSManagedObjectContext?

CBLIncrementalStore provides two helper methods that you could use to instantiate an NSManagedObjectContext object that will use the CBLIncrementalStore as its persistent store.

+ (NSManagedObjectContext*) createManagedObjectContextWithModel: (NSManagedObjectModel*)managedObjectModel
                                                   databaseName: (NSString*)databaseName
                                                          error: (NSError**)outError;
+ (NSManagedObjectContext*) createManagedObjectContextWithModel: (NSManagedObjectModel*)managedObjectModel
                                                   databaseName: (NSString*)databaseName
                                         importingDatabaseAtURL: (NSURL*)importUrl
                                                     importType: (NSString*)importType
                                                          error: (NSError**)outError;

You could also setup the CBLIncrementalStore and create the NSManagedObjectContext object by yourself. The only requirement is that the -updateManangedObjectModel: needs to be called to add the revision property into each entity defined by the given Core Data model. You could follow the code inside those two helper methods as a general guideline as well.

How to access CBLIncrementalStore and CBLDatabase object for setting up a replication?

If you create CBLIncrementalStore by yourself, you would have a direct access to it. However, if you use the helper method to set thing up, you can access the store object from the NSManagedObjectContext that you create as follows:

  CBLIncrementalStore* store = self.context.persistentStoreCoordinator.persistentStores.firstObject;
  CBLDatabase* database = store.database;
  CBLReplication* pull = [database createPullReplication: url];
  CBLReplication* push = [database createPushReplication: url];

Threading Model

By default, the CBLIncrementalStore will use the main thread to perform database operations. However sometimes this could slow down your user interface depending on how heavy your database operations are. CBLIncrementalStore provides a way that you can override its underlining CBLManager object to which you can setup your own dispatch queue for the database operations to be executed on.

  CBLManager* manager = [[CBLManager alloc] init];
  manager.dispatchQueue = self.queue;
  [CBLIncrementalStore setCBLManager: manager];

Please note that the method to setup the CBLManager object should be called before you create the CBLIncrementalStore object.

4. Resources :

Clone this wiki locally