Skip to content

Instantly share code, notes, and snippets.

@jkhowland
Created March 9, 2015 09:47
Show Gist options
  • Save jkhowland/213b4748d0fe88f07779 to your computer and use it in GitHub Desktop.
Save jkhowland/213b4748d0fe88f07779 to your computer and use it in GitHub Desktop.
PrivateDB, Adding and Entry, Updating and Entry, and Removing and Entry
#pragma mark - Debugging Methods
- (void)retrieveEntriesFromCloudKit {
// Simple request to check to see what is on CloudKit
NSPredicate *truePredicate = [NSPredicate predicateWithValue:YES];
CKQuery *query = [[CKQuery alloc] initWithRecordType:EntryRecordKey predicate:truePredicate];
[[EntryController privateDB] performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (!error) {
for (CKRecord *entry in results) {
NSLog(@"%@", entry);
}
}
}];
}
+ (CKDatabase *)privateDB {
CKDatabase *database = [[CKContainer defaultContainer] privateCloudDatabase];
return database;
}
- (void)addEntryWithTitle:(NSString *)title text:(NSString *)text date:(NSDate *)date {
CKRecord *cloudKitEntry = [[CKRecord alloc] initWithRecordType:EntryRecordKey];
cloudKitEntry[EntryIdentifierKey] = [[NSUUID UUID] UUIDString];
cloudKitEntry[EntryTitleKey] = title;
cloudKitEntry[EntryTextKey] = text;
cloudKitEntry[EntryTimeStampKey] = date;
[[EntryController privateDB] saveRecord:cloudKitEntry completionHandler:^(CKRecord *record, NSError *error) {
if (!error) {
NSLog(@"Saved Entry to CloudKit");
[self storeRecordToCoreData:cloudKitEntry uploaded:YES];
} else {
NSLog(@"NOT Saved Entry to CloudKit");
[self storeRecordToCoreData:cloudKitEntry uploaded:NO];
}
}];
}
- (void)storeRecordToCoreData:(CKRecord *)record uploaded:(BOOL)uploaded {
Entry *coreDataEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry"
inManagedObjectContext:[Stack sharedInstance].managedObjectContext];
coreDataEntry.identifier = record[EntryIdentifierKey];
coreDataEntry.title = record[EntryTitleKey];
coreDataEntry.text = record[EntryTextKey];
coreDataEntry.timestamp = record[EntryTimeStampKey];
coreDataEntry.uploaded = [NSNumber numberWithBool:uploaded];
[self synchronize];
}
- (void)updateEntry:(Entry *)entry {
NSPredicate *identifierPredicate = [NSPredicate predicateWithFormat:@"identifier == %@", entry.identifier];
CKQuery *query = [[CKQuery alloc] initWithRecordType:EntryRecordKey predicate:identifierPredicate];
[[EntryController privateDB] performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (!error) {
CKRecord *cloudKitEntry = nil;
if (results.count > 0) {
cloudKitEntry = results.firstObject;
} else {
cloudKitEntry = [[CKRecord alloc] initWithRecordType:EntryRecordKey];
}
cloudKitEntry[EntryTitleKey] = entry.title;
cloudKitEntry[EntryTextKey] = entry.text;
cloudKitEntry[EntryTimeStampKey] = entry.timestamp;
[[EntryController privateDB] saveRecord:cloudKitEntry completionHandler:nil];
}
}];
}
- (void)removeEntry:(Entry *)entry {
NSPredicate *identifierPredicate = [NSPredicate predicateWithFormat:@"identifier == %@", entry.identifier];
CKQuery *query = [[CKQuery alloc] initWithRecordType:EntryRecordKey predicate:identifierPredicate];
[[EntryController privateDB] performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (!error) {
CKRecord *cloudKitEntry = nil;
if (results.count > 0) {
cloudKitEntry = results.firstObject;
[[EntryController privateDB] deleteRecordWithID:cloudKitEntry.recordID completionHandler:nil];
}
}
}];
[entry.managedObjectContext deleteObject:entry];
[self synchronize];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment