Apparently we can't because it keeps throwing error whenever I try to set my own user record ID
to create CKRecordID
in order to create a record.
If we can't use each user's unique recordID
for record name, what's the point in setting its name in the first place? And also, how do we distinguish between users when fetching data?
CKRecordID *artworkRecordID = [[CKRecordID alloc] initWithRecordName:RANDOM_UNIQUE_NUMBER];
CKRecord *artworkRecord = [[CKRecord alloc] initWithRecordType:@"person" recordID:recordID];
artworkRecord[@"language"] = @"English";
CKContainer *myContainer = [CKContainer defaultContainer];
CKDatabase *privateDatabase = [myContainer privateCloudDatabase];
[privateDatabase saveRecord:artworkRecord completionHandler:^(CKRecord *artworkRecord, NSError *error){
if (!error) {
NSLog(@"Success!");
}
else {
NSLog(@"Failure!");
}
}];
EDIT
I've worked it out. So when you save/add data, record name doesn't matter at all because you'd only get your own data from PRIVATE DATA using your own user record ID hence you could set it as @"whatever" and it wouldn't matter at all.
I still don't understand a few things.
Why do we have to set record name for private data when we don't even use that in order to fetch data?
-(void)fetchRecord {
CKDatabase *privateDatabase = [[CKContainer defaultContainer] privateCloudDatabase];
CKReference *reference = [[CKReference alloc] initWithRecordID:self.cloudRecordID action:CKReferenceActionNone];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatorUserRecordID = %@", reference];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"person" predicate:predicate];
[privateDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (error) {
NSLog(@"error: %@",error.localizedDescription);
}
else {
if (![results firstObject]) {
}
else {
for (CKRecord *record in results) {
NSLog(@"%@",[record objectForKey:@"language"]);
}
}
}
}];}
Does not work. Settings the record ID ist just for updating an existing one without fetching it. So you can create a "person" and save it's CKRecordID (or name and zone of it) to your NSUserDefaults or the private cloud database for example. Or you save the UUID to the person object, so you can identify a person by device...
You don't have to distinguish the users. If you work on the private space, it's only for the user and you won't see data of others. And on the public space all belongs to all users of your app. (See CKContainer privateCloudDatabase and publicCloudDatabase)
If you want to handle belongings in public, use an own attribute.