Cloud Kit Subscribe to CKRecord Created
I have an iOS app. I am working on using Cloud Kit. What basically happens is that when the CKRecord is deleted, the person who created the CKRecord should be notified. For this, I create a subscription.
Everything seems to work as it should, but it seems that whenever you create a post, you are actually subscribing to all the posts. Then it doesn't matter who created the entry, because you will be notified. I think I am wrong about NSPredicate, maybe someone can help? Here is the code I have that matters:
NSString *attributeName = @"recordID";
NSString *attributeValue = newRecord.recordID.recordName;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ like %@", attributeName, attributeValue];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Photo" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordDeletion];
Now I understand that the attributeName for recordId should actually be something like recordID.recordName, but I tried that too and it didn't work. I still get the same result. I don't know how to do this, but basically I want the subscription to light up only to the creator of the CKRecord when that entry has been deleted.
Please note that I build first
CKRecord
and then upload it to CloudKit. Then in the callback method, I subscribe and do the code above.
source to share
When you ask for a recordID field, the attribute attribute must also be a recordID, not a string. Then for filter attributeName you shouldn't use% @ but% K
NSString *attributeName = @"recordID";
CKRecordID *attributeValue = [[CKRecordID alloc] initWithName:newRecord.recordID.recordName];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", attributeName, attributeValue];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Photo" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordDeletion];
But when you have this kind of logic, you will create a subscription for every post you create. You can solve this problem with one general subscription. Just add a condition for the creator of the UserRecordID metadata field. Then you need to first extract the iCloud account login ID and then create a subscription like this:
execute fetchUserRecordIDWithCompletionHandler in container. In the callback, you will get the post ID. just use this to create a filter
[[CKContainer defaultContainer] fetchUserRecordIDWithCompletionHandler:^(CKRecordID *recordID, NSError *error) {
NSString *attributeName = @"creatorUserRecordID";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", attributeName, recordID];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Photo" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordDeletion];
// ... rest of your code
}]
Just extend this code with error handling
source to share