Backendless Support
 
Answered

How to update data object in backendless table in swift

Hello,

I am working with backendless.

After saving data object in backendless table, I can't update data object.

I have tried several times, but, I have failed.

I have checked following link

https://backendless.com/documentation/data/ios/data_updating_data_objects.htm

But, I don't understand well

here are sample code

func updateContactAsync(contact:Contact) { let dataStore = Backendless.sharedInstance().data.of(Contact.ofClass()) // update object asynchronously contact.title = "Superman" contact.phone = "212-555-5555" dataStore.save( contact, response: { (result: AnyObject!) -> Void in let updatedContact = result as! Contact print("Contact has been updated: \(updatedContact.objectId)") }, error: { (fault: Fault!) -> Void in print("Server reported an error (2): \(fault)") })}

In above code, we receive custom table from parameter.

I am sorry, Would you please explain how to get it?

Regards

Andrei

Leave a Comment

Comments (2)

photo
1

Here are my sample code

func uploadLoginUser(phone: String, results: @escaping ((_ success: Bool) -> Void)) {

let userInfo = UserInformation()

userInfo.userOneId = backendless!.userService.currentUser.objectId! as String

userInfo.phoneNumber = phone

userInfo.signIn = "true"

backendless!.data.of(UserInformation.ofClass())!.save(userInfo, response: { (result: Any?) -> () in

results(true)

ProgressHUD.dismiss()

}) { (fault) in

results(false)

ProgressHUD.showError("Error Sending activation code: \(fault!.detail!)")

}

}

photo
1

Hello Andrei,

Firstly, we have our Contact table with the string fields: title and phone and some records in it (screenshot 1).

Here is Contact class description in the Swift project:

  1. @objcMembers
  2. class Contact: NSObject {
  3. var title: String?
  4. var phone: String?
  5. }

The code below describes all steps of object update with the comments. The result is on the screenshot 2.

  1. func updateContact() {
  2. // first of all we're receiing the contact we want to update
  3. // e.g. it will be the contact with phone number '212-555-6666' (screenshot 1)
  4. let queryBuilder = DataQueryBuilder()!
  5. queryBuilder.setWhereClause("phone = '212-555-6666'")
  6. backendless.data.of(Contact.ofClass()).findFirst(
  7. queryBuilder,
  8. response: { contactThatWillBeUpdated in
  9. // and now we can update it with a new phone number '212-555-5555'
  10. (contactThatWillBeUpdated as! Contact).phone = "212-555-5555"
  11. // and save the updated contact
  12. self.backendless.data.of(Contact.ofClass()).save(
  13. contactThatWillBeUpdated,
  14. response: { alreadyUpdatedContact in
  15. print("Updated contact '\((alreadyUpdatedContact as! Contact).title!)' with a new phone number: \((alreadyUpdatedContact as! Contact).phone!)")
  16. }, error: {fault in
  17. print("Failed to update contact: \(fault!.message)")
  18. })
  19. },
  20. error: { fault in
  21. print("Failed to find the contact: \(fault!.message)")
  22. })
  23. }

Regards, Olga