How can I Delete from a CloudKit container?
Recently I've implemented CloudKit inside my app, I can successfully save the data on CloudKit and show it inside a TableView. The problem is that I can't remove the single data from the container.
Here's the code I use:
let database = CKContainer.default().privateCloudDatabase
var notes = [CKRecord]()
func saveToCloud(note: String)
let newQuote = CKRecord(recordType: "Note")
newQuote.setValue(note, forKey: "content")
database.save(newQuote) (record, error) in
guard record != nil else return
print("saved record")
@objc func queryDatabase()
let query = CKQuery(recordType: "Note", predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) (records, _) in
guard let records = records else return
let sortedRecords = records.sorted(by: $0.creationDate! > $1.creationDate! )
self.quotesSavedOnCloud = sortedRecords
DispatchQueue.main.async
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
And here's the part of the code that I want to be able to delete data with a swipe:
func deleteCloudData(recordName: String)
let recordID = CKRecord.ID(recordName: recordName)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordName: String)
print("Data delated successfully")
ios swift tableview cloudkit
add a comment |
Recently I've implemented CloudKit inside my app, I can successfully save the data on CloudKit and show it inside a TableView. The problem is that I can't remove the single data from the container.
Here's the code I use:
let database = CKContainer.default().privateCloudDatabase
var notes = [CKRecord]()
func saveToCloud(note: String)
let newQuote = CKRecord(recordType: "Note")
newQuote.setValue(note, forKey: "content")
database.save(newQuote) (record, error) in
guard record != nil else return
print("saved record")
@objc func queryDatabase()
let query = CKQuery(recordType: "Note", predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) (records, _) in
guard let records = records else return
let sortedRecords = records.sorted(by: $0.creationDate! > $1.creationDate! )
self.quotesSavedOnCloud = sortedRecords
DispatchQueue.main.async
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
And here's the part of the code that I want to be able to delete data with a swipe:
func deleteCloudData(recordName: String)
let recordID = CKRecord.ID(recordName: recordName)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordName: String)
print("Data delated successfully")
ios swift tableview cloudkit
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49
add a comment |
Recently I've implemented CloudKit inside my app, I can successfully save the data on CloudKit and show it inside a TableView. The problem is that I can't remove the single data from the container.
Here's the code I use:
let database = CKContainer.default().privateCloudDatabase
var notes = [CKRecord]()
func saveToCloud(note: String)
let newQuote = CKRecord(recordType: "Note")
newQuote.setValue(note, forKey: "content")
database.save(newQuote) (record, error) in
guard record != nil else return
print("saved record")
@objc func queryDatabase()
let query = CKQuery(recordType: "Note", predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) (records, _) in
guard let records = records else return
let sortedRecords = records.sorted(by: $0.creationDate! > $1.creationDate! )
self.quotesSavedOnCloud = sortedRecords
DispatchQueue.main.async
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
And here's the part of the code that I want to be able to delete data with a swipe:
func deleteCloudData(recordName: String)
let recordID = CKRecord.ID(recordName: recordName)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordName: String)
print("Data delated successfully")
ios swift tableview cloudkit
Recently I've implemented CloudKit inside my app, I can successfully save the data on CloudKit and show it inside a TableView. The problem is that I can't remove the single data from the container.
Here's the code I use:
let database = CKContainer.default().privateCloudDatabase
var notes = [CKRecord]()
func saveToCloud(note: String)
let newQuote = CKRecord(recordType: "Note")
newQuote.setValue(note, forKey: "content")
database.save(newQuote) (record, error) in
guard record != nil else return
print("saved record")
@objc func queryDatabase()
let query = CKQuery(recordType: "Note", predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) (records, _) in
guard let records = records else return
let sortedRecords = records.sorted(by: $0.creationDate! > $1.creationDate! )
self.quotesSavedOnCloud = sortedRecords
DispatchQueue.main.async
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
And here's the part of the code that I want to be able to delete data with a swipe:
func deleteCloudData(recordName: String)
let recordID = CKRecord.ID(recordName: recordName)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordName: String)
print("Data delated successfully")
ios swift tableview cloudkit
ios swift tableview cloudkit
edited Nov 15 '18 at 19:48
Giovanni Filippini
asked Nov 15 '18 at 19:38
Giovanni FilippiniGiovanni Filippini
168
168
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49
add a comment |
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49
add a comment |
1 Answer
1
active
oldest
votes
You can't pass String
to deleteCloudData
, you need to pass a specific string value - the record id for the given index path would be my guess given what you are trying to do.
Get the CKRecord
for the index path (just like are doing in cellForRowAt
), and get its recordID
.
BTW, it would make more sense for your deleteCloudData
to take a CKRecord.ID
and not a String
.
func deleteCloudData(recordID: CKRecord.ID)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordID: quotesSavedOnCloud[indexPath.row].recordID)
print("Data delated successfully")
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is aCKRecord
. It has arecordID
property. That's what you need to pass todeleteCloudData
.
– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326804%2fhow-can-i-delete-from-a-cloudkit-container%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't pass String
to deleteCloudData
, you need to pass a specific string value - the record id for the given index path would be my guess given what you are trying to do.
Get the CKRecord
for the index path (just like are doing in cellForRowAt
), and get its recordID
.
BTW, it would make more sense for your deleteCloudData
to take a CKRecord.ID
and not a String
.
func deleteCloudData(recordID: CKRecord.ID)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordID: quotesSavedOnCloud[indexPath.row].recordID)
print("Data delated successfully")
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is aCKRecord
. It has arecordID
property. That's what you need to pass todeleteCloudData
.
– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
add a comment |
You can't pass String
to deleteCloudData
, you need to pass a specific string value - the record id for the given index path would be my guess given what you are trying to do.
Get the CKRecord
for the index path (just like are doing in cellForRowAt
), and get its recordID
.
BTW, it would make more sense for your deleteCloudData
to take a CKRecord.ID
and not a String
.
func deleteCloudData(recordID: CKRecord.ID)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordID: quotesSavedOnCloud[indexPath.row].recordID)
print("Data delated successfully")
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is aCKRecord
. It has arecordID
property. That's what you need to pass todeleteCloudData
.
– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
add a comment |
You can't pass String
to deleteCloudData
, you need to pass a specific string value - the record id for the given index path would be my guess given what you are trying to do.
Get the CKRecord
for the index path (just like are doing in cellForRowAt
), and get its recordID
.
BTW, it would make more sense for your deleteCloudData
to take a CKRecord.ID
and not a String
.
func deleteCloudData(recordID: CKRecord.ID)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordID: quotesSavedOnCloud[indexPath.row].recordID)
print("Data delated successfully")
You can't pass String
to deleteCloudData
, you need to pass a specific string value - the record id for the given index path would be my guess given what you are trying to do.
Get the CKRecord
for the index path (just like are doing in cellForRowAt
), and get its recordID
.
BTW, it would make more sense for your deleteCloudData
to take a CKRecord.ID
and not a String
.
func deleteCloudData(recordID: CKRecord.ID)
database.delete(withRecordID: recordID) (id, error) in
if error != nil
print(error.debugDescription)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == UITableViewCell.EditingStyle.delete
deleteCloudData(recordID: quotesSavedOnCloud[indexPath.row].recordID)
print("Data delated successfully")
edited Nov 15 '18 at 20:19
answered Nov 15 '18 at 19:52
rmaddyrmaddy
246k27327390
246k27327390
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is aCKRecord
. It has arecordID
property. That's what you need to pass todeleteCloudData
.
– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
add a comment |
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is aCKRecord
. It has arecordID
property. That's what you need to pass todeleteCloudData
.
– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
For show the data inside the TableView I use: let quoteToDelete = quotesSavedOnCloud[indexPath.row].value(forKey: "content") as! String
– Giovanni Filippini
Nov 15 '18 at 20:07
quotesSavedOnCloud[indexPath.row]
is a CKRecord
. It has a recordID
property. That's what you need to pass to deleteCloudData
.– rmaddy
Nov 15 '18 at 20:09
quotesSavedOnCloud[indexPath.row]
is a CKRecord
. It has a recordID
property. That's what you need to pass to deleteCloudData
.– rmaddy
Nov 15 '18 at 20:09
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
And how can I call it?
– Giovanni Filippini
Nov 15 '18 at 20:15
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
See my updated answer.
– rmaddy
Nov 15 '18 at 20:19
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
It works, but I need to refresh the Tableview in order to make the data disappear
– Giovanni Filippini
Nov 15 '18 at 20:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326804%2fhow-can-i-delete-from-a-cloudkit-container%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I've updated the question with the code that I'm using right now
– Giovanni Filippini
Nov 15 '18 at 19:44
Now that you’ve shown some code, what problem are you having with it?
– rmaddy
Nov 15 '18 at 19:45
When I call the function deleteCloudData(recordName: String) Xcode gave me this error: Cannot convert value of type 'String.Type' to expected argument type 'String'
– Giovanni Filippini
Nov 15 '18 at 19:49