Reputation: 3
For the app I'm working on, a user can create an account and can create posts for other users to see.
I want to provide the option for users to delete their account & posts but see that the record deletion function requires the object's recordID (https://developer.apple.com/documentation/cloudkit/ckdatabase/1449122-delete).
Is there a way I can retrieve and save the CloudKit generated recordID metadata for records, so the recordIDs can be utilized for record deletion later?
I've tried things like the following to no avail:
let rN = record.object(forKey: "recordName") as? String ?? nil
if let recordName = rN
{
personP.recordID = CKRecord.ID.init(recordName: recordName)
}
For some more context, I've been using a separate String field as an identifier for users to fetch their existing account/posts when they log into the app.
Alternatively: is there a way to delete records without needing the CKrecordID (for example, being able to delete all records returned from a CKQueryOperation)?
Any guidance or suggestions would be greatly appreciated.
Upvotes: 0
Views: 207
Reputation: 3
Found that you can leverage the NSManagedObjectID of an entity to obtain its recordID (https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3141669-recordid)
Example use for creating an array of recordIDs for an entity called "Post":
var recordIDArray = [CKRecord.ID]()
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Post")
let requestPredicate = NSPredicate(value: true)
fetchRequest.predicate = requestPredicate
moc.performAndWait
{
let result = try? moc.fetch(fetchRequest)
for data in result as! [NSManagedObject]
{
let recordID = CoreDataModel.persistenceContainer.recordID(for:data.objectID)
if let rID = recordID
{
recordIDArray.append(rID)
print("record ID for a post is \(String(describing: rID))")
}
}
}
Upvotes: 0