-
-
Save wagpinto/567a4e06cae60388858e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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); | |
} | |
} | |
}]; | |
} |
This file contains hidden or 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
+ (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