Tutorial for NimbusBase iOS Sync

Enable Sync

NimbusBase’s code to enable sync is similar to iCloud, so you can work with it easily.

First, you need to config NimbusBase and bind it to NSPersistentStoreCoordinator. By replacing initializer initWithManagedObjectModel:, you use a category method:

_persistentStoreCoordinator = 
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel
               	                                   nimbusConfigs:self.nimbusBaseConfigs];

Second, let NimbusBase know what changes you are making:

[self.persistentStoreCoordinator.nimbusBase trackChangesOfMOContext:_managedObjectContext];

And acquire the changes that NimbusBase is making:

// Same changes import callback with iCloud
- (void)handlePersistentStoreDidImportUbiquitousContentChangesNotification:(NSNotification *)notification {
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}

Then, you are ready to go:

[self.persistentStoreCoordinator.nimbusBase.defaultServer synchronize];

More powerful and interactive Sync

NimbusBase offers several methods and notifications so that your UI can utilize it to get sync results.

To acquire the progress of a sync, you can use the general callback of the NMBPromise returned by method synchronize:

NMBServer *server = self.persistentStoreCoordinator.nimbusBase.defaultServer;
NMBPromise *syncPromise = [server synchronize];
[syncPromise progress:^(NMBPromise *promise, float progress) {
    NSLog(@"My sync has been completed %3.0f%%", progress);
}];

To get notifications when a sync succeeds or fails, you can register on NSNotificationCenter:

- (void)addObserverForSync {
    NSNotificationCenter *ntfCntr = [NSNotificationCenter defaultCenter];
    [ntfCntr addObserver:self
                selector:@selector(handleNMBServerSyncDidSucceed:)
                    name:NMBNotiSyncDidSucceed
                  object:nil];
    [ntfCntr addObserver:self
                selector:@selector(handleNMBServerSyncDidFail:)
                    name:NMBNotiSyncDidFail
                  object:nil];
}

- (void)handleNMBServerSyncDidSucceed:(NSNotification *)notification {
    NSLog(@"Oh yeah, the sync of NMBServer %@ has been completed.", notification.object);
}

- (void)handleNMBServerSyncDidFail:(NSNotification *)notification {
    NSLog(@"Oops, something goes wrong with the sync of NMBServer %@. Reason: %@", 
    	  notification.object, 
    	  notification.userInfo[NKeyNotiError]);
}

The method synchronize accepts options by using the following form:

NMBServer *server = self.persistentStoreCoordinator.nimbusBase.defaultServer;
[server synchronizeWithOptions:@{NKeySyncCommitType: @(NMBSyncCommitTypePullEntirely)}];

You will find this useful when you want to rebuild database from cloud. For more information see Migration Tutorial.