nachshon fertel
nachshon fertel

Reputation: 80

Deleting data from cloudkit with swift

How do I delete some data I put into the cloud? I made an app that when you enter a url in 1 view controller it uploads into the cloud, and when you go to another view controller it shows the url you entered before from the cloud in a webview. for example, when you enter www.hello.com, it goes into the cloud, and when you go to the webview on a different view controller it loads www.hello.com.

My problem:

When I enter a second url, it still loads the first one. How do I get it to load the latest one I uploaded? Is there a way to delete the first one when the second one is uploaded?

Upvotes: 4

Views: 3731

Answers (1)

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

You can delete records with code like this:

database.deleteRecordWithID(CKRecordID(recordName: recordId), completionHandler: {recordID, error in 
 NSLog("OK or \(error)")
}

where database is the CKDatabase that you are using.

But in your situation it might be better to update the previous created record. An other solution would be to query your data while using a sort order on the creationDate like this:

query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

Then just pick the first one since that's the last one you saved. A nice extra is that you would have a history in your database.

Upvotes: 5

Related Questions