Created
April 17, 2015 11:23
-
-
Save xpqz/62b5267c250f04c30f9b to your computer and use it in GitHub Desktop.
Cloudant helper class .m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Cloudant.m | |
// | |
// | |
// Created by Stefan Kruger on 12/03/2015. | |
// | |
// | |
#import "Cloudant.h" | |
@interface Cloudant () | |
@property (strong, nonatomic) CDTDatastore *datastore; | |
@property (strong, nonatomic) CDTReplicatorFactory *replicatorFactory; | |
@property (nonatomic, strong) CDTReplicator *pullReplicator; | |
@property (nonatomic, strong) CDTReplicator *pushReplicator; | |
@end | |
@implementation Cloudant | |
- (instancetype)init { | |
if (self = [super init]) { | |
// Standard boiler-plate: create a CDTDatastoreManager using application internal storage path | |
NSError *error = nil; | |
NSFileManager *fileManager= [NSFileManager defaultManager]; | |
NSURL *documentsDir = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; | |
NSURL *storeURL = [documentsDir URLByAppendingPathComponent:@"cloudant-sync-datastore"]; | |
NSString *path = [storeURL path]; | |
CDTDatastoreManager *manager = [[CDTDatastoreManager alloc] initWithDirectory:path error:&error]; | |
self.datastore = [manager datastoreNamed:@"my_datastore" error:&error]; | |
// We need at least one index in order to use Cloudant Query. We put an index on the guaranteed to exist field "_id" | |
[self.datastore ensureIndexed:@[@"_id"] withName:@"all"]; | |
self.replicatorFactory = [[CDTReplicatorFactory alloc] initWithDatastoreManager:manager]; | |
} | |
return self; | |
} | |
- (BOOL)replicateFromURL:(NSURL *)remoteDatabaseURL delegate:(id<CDTReplicatorDelegate>)dlg error:(NSError *__autoreleasing *)error { | |
CDTPullReplication *pullReplication = [CDTPullReplication replicationWithSource:remoteDatabaseURL target:self.datastore]; | |
self.pullReplicator = [self.replicatorFactory oneWay:pullReplication error:error]; | |
if (self.pullReplicator != nil) { | |
if (dlg != nil) { | |
self.pullReplicator.delegate = dlg; | |
} | |
} else { | |
return false; | |
} | |
// Start the replication | |
return [self.pullReplicator startWithError:error]; | |
} | |
- (BOOL)replicateToURL:(NSURL *)remoteDatabaseURL delegate:(id<CDTReplicatorDelegate>)dlg error:(NSError *__autoreleasing *)error { | |
CDTPushReplication *pushReplication = [CDTPushReplication replicationWithSource:self.datastore target:remoteDatabaseURL]; | |
self.pushReplicator = [self.replicatorFactory oneWay:pushReplication error:error]; | |
if (!self.pushReplicator) { | |
return nil; | |
} | |
if (dlg != nil) { | |
self.pushReplicator.delegate = dlg; | |
} | |
// Start the replication | |
return [self.pushReplicator startWithError:error]; | |
} | |
- (CDTQResultSet *)find:(NSDictionary *)query { | |
return [self.datastore find:query]; | |
} | |
- (CDTDocumentRevision *)saveDocumentWithBody:(NSDictionary *)body error:(NSError *__autoreleasing *)error { | |
CDTMutableDocumentRevision *rev = [CDTMutableDocumentRevision revision]; | |
rev.body = body; | |
CDTDocumentRevision *doc = [self.datastore createDocumentFromRevision:rev error:error]; | |
if (doc == nil) { | |
return nil; | |
} | |
NSLog(@"ID: %@", doc.docId); | |
return doc; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment