Can't add two users one after another.

Hi,
How are you doing? Im new to Backendless and I really like your interface. However, I have some struggles. The problem is about registering user and getting the response. I use Swift.
Im trying to connect a User to a Person.
First, I was getting the user response as nil. Then, I solved the problem (don’t know how :slight_smile: ). I can add a User, a Person and add User to that Person.
Now, when I try to save another user (with different email - identity) after I create one, I get the User as nil.
Thank you in advance.
Görkem
P.S.: Code is attached, as well.

// Registers user to Backendless
private func registerUser() {
 
 let user = BackendlessUser()
 user.name = self.nameTextField.text! as NSString
 user.email = self.emailTextField.text! as NSString
 user.password = self.passwordTextField.text! as NSString
 
 backendless.userService.register(user, response: { (newUser) in
      self.addUserAsPerson(user: newUser)
      self.informationLabel.text = "User registered successfully."



    }) { (fault) in
      print(fault!)
      self.informationLabel.text = "Error: \(String(describing: fault?.description))"
   }
}



// Adds user to People table
private func addUserAsPerson(user:BackendlessUser?) {
      print("userId: \(user?.objectId)")
 
      let newPerson = People()
      newPerson.birthYear = Int(self.birthyearTextField.text!) as NSNumber?
 
      let dataStore = backendless.data.ofTable("People")
      dataStore?.save(newPerson,
            response: {
                  (result) -> () in
 
                        backendless.data.of(People.ofClass()).setRelation("user:Users:1", parentObjectId:                               (result as! People).objectId, childObjects: [user?.objectId as Any], response: {                                     response) in
                        }, error: { (fault) in
                              print(fault!)
                        })
                  },
                        error: {
                        (fault : Fault?) -> () in
                        print(fault!)
            })
}


// Console Output:
userId: Optional(DB774EE6-4457-95FF-FF83-2692EBA83100)
userId: nil
FAULT = '1303' [Unable to create relation. Child object with id 'null' is not found in the related table.] <Unable to create relation. Child object with id 'null' is not found in the related table.>

code and console.txt (1.71kB)

Hi Chat (or Görkem?),

Could you please let me know where the second “userId: nil” in the console output comes from?

If that objectId is indeed nil, it explains why setting the relationship fails, but by looking at the code, I see that there should be only one line printed out with the user’s objectId.

Regards,
Mark

Hi Mark,

It’s Görkem :slight_smile: Thank you for quick answer.

When I register user, registerUser() function is called. In the view attached, when I click on save, it registers User, creates a Person and adds that user to the Person created (prints userId as seen - console output #2). Then, I change the inputs (on the view), and click save again. So the function is called twice. During the second registration, it creates another User with the new e-mail addres (id), another Person but, because the response user is nil (prints nil - console output #3), it cannot add the 2nd User to 2nd Person. And we got an error (console output #4).

I hope I’ve explained well.

Hi Görkem,

Since you get nil from user registration, we should focus on that issue, since that’s not the expected response. The fact that relation is not created is a negative side-effect of a previously failed operation.

Could you please isolate the use-case when the returned value is nil?

Regards,
Mark

Hi Mark,

I didn’t understand the isolation of use-case. Here is another log, maybe it will help.

 
 
 
 
 
 
-------
 
new User is created: Optional(22AB4F23-B8B2-B6B7-FF13-3B5951AA1800) 
 
new person is created: Optional("D48F2FD8-C3F9-AF65-FFEE-FCAACF19FA00") 
 
user-person relationship is created between user: Optional(22AB4F23-B8B2-B6B7-FF13-3B5951AA1800) and person: Optional("D48F2FD8-C3F9-AF65-FFEE-FCAACF19FA00")) 
 
------- 
 
------- 
 
new User is created: nil 
 
new person is created: Optional("6EC9B1D1-ABBD-DF65-FFE2-A0CFC15B2900") 
 
user-person relationship couldn't be created. 
 
------- 
 
FAULT = '1303' [Unable to create relation. Child object with id 'null' is not found in the related table.] <Unable to create relation. Child object with id 'null' is not found in the related table.> 


Unfortunately, it does not help because it is not clear how you’re creating the user when you get back null. What is different about the second time you call the method?

Nothing. I just change the e-mail address. I can send you the code if you like… Here is the class’s code:

 
 
 
 
 
 
class RegisterTableViewController: UITableViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
 
 
 
 @IBOutlet weak var informationLabel: UILabel! 
 
 
 
 @IBOutlet weak var informationSymbolLabel: UILabel! 
 
 @IBOutlet weak var nameSymbolLabel: UILabel! 
 
 @IBOutlet weak var emailSymbolLabel: UILabel! 
 
 @IBOutlet weak var passwordSymbolLabel: UILabel! 
 
 @IBOutlet weak var birthyearSymbolLabel: UILabel! 
 
 
 
 @IBOutlet weak var nameTextField: UITextField! 
 
 @IBOutlet weak var emailTextField: UITextField! 
 
 @IBOutlet weak var passwordTextField: UITextField! 
 
 @IBOutlet weak var birthyearTextField: UITextField! 
 
 
 
 private var birthYearPickerView: UIPickerView! = UIPickerView() 
 
 private var pickerValues = [String]() 
 
 
 
 
 override func viewDidLoad() { 
 
 super.viewDidLoad() 
 
 
 
 self.initSymbols() 
 
 self.initPickerView() 
 
 
 
 // Uncomment the following line to preserve selection between presentations 
 
 // self.clearsSelectionOnViewWillAppear = false 
 
 
 
 
 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
 
 // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
 
 } 
 
 
 
 
 override func didReceiveMemoryWarning() { 
 
 super.didReceiveMemoryWarning() 
 
 } 
 
 
 
 // MARK: - Init Symbols 
 
 private func initSymbols() { 
 
 self.setInformationCell(alpha: 0.0, color: .clear) 
 
 self.informationSymbolLabel.text = String(format: "%C", faicon["fa-exclamation"]!) 
 
 self.nameSymbolLabel.text = String(format: "%C", faicon["fa-user"]!) 
 
 self.emailSymbolLabel.text = String(format: "%C", faicon["fa-envelope"]!) 
 
 self.passwordSymbolLabel.text = String(format: "%C", faicon["fa-key"]!) 
 
 self.birthyearSymbolLabel.text = String(format: "%C", faicon["fa-birthday-cake"]!) 
 
 } 
 
 
 
 // Registers user to Backendless 
 
 private func registerUser() { 
 
 
 
 let user = BackendlessUser() 
 
 user.name = self.nameTextField.text! as NSString 
 
 user.email = self.emailTextField.text! as NSString 
 
 user.password = self.passwordTextField.text! as NSString 
 
 
 
 backendless.userService.register(user, response: { (newUser) in 
 
 print("-------") 
 
 
 
 
 print("new User is created: \(String(describing: newUser?.objectId))") 
 
 
 
 self.addUserAsPerson(user: newUser) 
 
 self.informationLabel.text = "User registered successfully." 
 
 
 
 
 }) { (fault) in 
 
 print(fault!) 
 
 self.informationLabel.text = "Error: \(String(describing: fault?.description))" 
 
 } 
 
 } 
 
 
 
 // Adds user to People table 
 
 private func addUserAsPerson(user:BackendlessUser?) { 
 
 
 
 let newPerson = People() 
 
 newPerson.birthYear = Int(self.birthyearTextField.text!) as NSNumber? 
 
 
 
 let dataStore = backendless.data.ofTable("People") 
 
 dataStore?.save(newPerson, 
 
 response: { 
 
 (result) -> () in 
 
 
 
 print("new person is created: \(String(describing: (result as! People).objectId))") 
 
 
 
 backendless.data.of(People.ofClass()).setRelation("user:Users:1", parentObjectId: (result as! People).objectId, childObjects: [user?.objectId as Any], response: { (response) in 
 
 
 
 print("user-person relationship is created between user: \(String(describing: user?.objectId)) and person: \(String(describing: (result as! People).objectId)))") 
 
 print("-------") 
 
 
 
 
 
 
 }, error: { (fault) in 
 
 
 
 print("user-person relationship couldn't be created.") 
 
 print("-------") 
 
 
 
 
 print(fault!) 
 
 }) 
 
 
 
 }, 
 
 error: { 
 
 (fault : Fault?) -> () in 
 
 print("Server reported an error: \(String(describing: fault))") 
 
 }) 
 
 
 
 
 } 
 
 
 
 private func initPickerView() { 
 
 birthYearPickerView.delegate = self 
 
 birthYearPickerView.dataSource = self 
 
 
 
 let toolBar = UIToolbar() 
 
 toolBar.barStyle = UIBarStyle.default 
 
 toolBar.isTranslucent = true 
 
 toolBar.tintColor = blueColor 
 
 toolBar.sizeToFit() 
 
 
 
 birthYearPickerView.backgroundColor = UIColor.white 
 
 
 
 let nextButton = UIBarButtonItem(title: NSLocalizedString("global-next", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(RegisterTableViewController.toolBarNext)) 
 
 let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
 
 let cancelButton = UIBarButtonItem.init(barButtonSystemItem: .stop, target: self, action: #selector(RegisterTableViewController.toolBarCancel)) 
 
 
 
 toolBar.setItems([cancelButton, spaceButton, nextButton], animated: true) 
 
 toolBar.isUserInteractionEnabled = true 
 
 
 
 birthyearTextField.inputView = birthYearPickerView 
 
 birthyearTextField.inputAccessoryView = toolBar 
 
 } 
 
 
 
 
 @objc private func toolBarNext() { 
 
 saveUser(self) 
 
 } 
 
 
 
 @objc private func toolBarCancel() { 
 
 birthyearTextField.resignFirstResponder() 
 
 } 
 
 
 
 
 
 
 
 @IBAction func saveUser(_ sender: Any) { 
 
 self.setInformationCell(alpha: 0.0, color: .clear) 
 
 
 
 if self.nameTextField.text != "" /*&& validateEmail(email: self.emailTextField.text!) */&& self.passwordTextField.text != "" { 
 
 self.registerUser() 
 
 } else { 
 
 self.setInformationCell(alpha: 1.0, color: .red) 
 
 self.informationLabel.text = "Error: All fields are required." 
 
 } 
 
 
 
 self.updateTable(duration: 0.3) 
 
 }


If you could zip up the project directory and share it with us, we’d be able to debug it on our side. Please upload the zip file to either google drive or dropbox and email the link to support@backendless.com

Thanks!
Mark

Hi Mark,

I sent the e-mail.

This issue has been fixed. Please update to the newest version of iOS-SDK (4.0.5) and verify wether everything works fine.

Regards, Olga

Thank you Olga. It works!!

Thanks to you too Mike, for your help :slight_smile: