Updating existing users in swift

I am following your instructions for updating current users. I have a user logged into the app. They click on the button to update their profile. Here is the code.

@IBAction func saveChanges(sender: AnyObject) {
 
 Types.tryblock({ () -> Void in
 
 let user = BackendlessUser()
 user.password = self.passWord.text
 user.name = self.newUserName.text
 user.email = self.newEmail.text
 
 let registeredUser = self.backendless.userService.registering(user)
 print("User has been registered (SYNC): \(registeredUser)")
 
 let properties = [
 "name" : user.name,
 "email" : user.email,
 "password" : user.password
 ]
 
 registeredUser.updateProperties( properties )
 let updatedUser = self.backendless.userService.update(user)
 print("User updated (SYNC): \(updatedUser)")
 
 },
 
 catchblock: { (exception) -> Void in
 print("Server reported an error: \(exception)" )
 })


 }

My problem is that this created a new user.
If I do it like this, which make more sense…

let properties = [
 "name" : self.passWord.text! as String,
 "email" : self.newUserName.text! as String,
 "password" : self.newEmail.text! as String
 ]

I get this error -

Server reported an error: FAULT = '3102' [user credentials is not valid] <backendless user credentials is not valid> 

Any help on updating current users profile would be appreciated.

Hi Eric,

What line in the example gives you that exception?

Also, when you said “My problem is that this created a new user.”, the code creates a new user on line 10 and then updates the user on line 20. What were you referring to?

Regards,
Mark

I took out the line where it added a new user. I changed it to…

 let registeredUser = self.backendless.userService.currentUser

When I run the app, try and update the user name of the current user, I get this error in the console.

Server reported an error: FAULT = '3024' [Unable to update user account due to error: Can not get user schema] <Unable to update user account due to error: Can not get user schema> 

This is different from the error in the documentation under Update User Properties.

3024 General "update registration" error. Error message should contain
additional details.

Thanks for any help you can offer.
Eric

Hi Eric,

In BackendlessUser class the property ‘email’ is ‘Identity’ (by default):
http://support.backendless.com/public/attachments/e78d322d3766b5d5377164d5dc02b615.png</img>

so you MAY NOT update the identity.

In your case you can go by one of the ways:

  1. Don’t change ‘email’ property, because it is Identity
  2. Set another property as Identity

Regards,
Slava

Here’s the code I just tried in my app and didn’t get any errors at all:

        backendless.userService.login("mark@backendless.com", password: "password" );
        let currentUser = backendless.userService.currentUser;
        
        currentUser.setProperty("name", object: "Mark Piller" );
        let updatedUser = backendless.userService.update(currentUser)
        let updatedName = updatedUser.getProperty("name")
        print( "user has been updated. Name \(updatedName)" )

Mark,

Changing user name directly in the code works. I am trying to take input from the text fields to allow the user to update their username. Even if I take your code such as this -

 @IBAction func saveChanges(sender: AnyObject) {
        
        Types.tryblock({ () -> Void in
            
            let user = BackendlessUser()
            user.email = "echamberlin@gocva.com"
            user.password = "16France"
            
            let registeredUser = self.backendless.userService.currentUser
            print("User has been registered (SYNC): \(registeredUser)")
            
            let properties = [
                "name" : "my new name is Eric"
            ]
            
            registeredUser.updateProperties( properties )
            let updatedUser = self.backendless.userService.update(user)
            print("User updated (SYNC): \(updatedUser)")
            
            },
            
            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception)" )
        })
            }

When I click the button, I get this output.

<BackendlessUser> {
    "__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"password\",\"created\",\"___class\",\"name\",\"ownerId\",\"updated\",\"objectId\",\"email\",\"__meta\"],\"relatedObjects\":{}}";
    created = "2016-02-09 08:46:34 +0000";
    email = "";
    lastLogin = "2016-02-08 15:12:12 +0000";
    name = "This is my new name";
    objectId = "BB1D674A-4B4F-4AF8-FFAB-914D945B1800";
    ownerId = "E6C72B59-1A8F-FC3D-FF1F-776D6EC87600";
    password = 16France;
    updated = "2016-02-09 08:46:34 +0000";
    "user-token" = "52323D3B-6FE0-6C14-FF2B-8749379F3D00";
}
Server reported an error: FAULT = '3024' [Unable to update user account due to error: Can not get user schema] <Unable to update user account due to error: Can not get user schema> 

del

Slava,

I read the page you sent me regarding the previous post and it did not offer much of a help.
I just tried this …

@IBAction func saveChanges(sender: AnyObject) {
        
        Types.tryblock({ () -> Void in
            
            let user = BackendlessUser()
            user.email = "echamberlin@gocva.com"
            user.password = "16France"
            
            let registeredUser = self.backendless.userService.currentUser
            print("User has been registered (SYNC): \(registeredUser)")
            
            let properties = [
                "name" : self.newUserName.text! as String
            ]
            
            registeredUser.updateProperties( properties )
            let updatedUser = self.backendless.userService.update(user)
            print("User updated (SYNC): \(updatedUser)")
            
            },
            
            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception)" )
        })
            }

and I get this…after the server pauses for 3-5 seconds.









<BackendlessUser> {

    "__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"password\",\"created\",\"___class\",\"name\",\"ownerId\",\"updated\",\"objectId\",\"email\",\"__meta\"],\"relatedObjects\":{}}";

    created = "2016-02-09 08:46:34 +0000";

    email = "";

    "first_name" = Chris;

    lastLogin = "2016-02-08 15:12:12 +0000";

    name = "my new name is Eric";

    objectId = "BB1D674A-4B4F-4AF8-FFAB-914D945B1800";

    ownerId = "E6C72B59-1A8F-FC3D-FF1F-776D6EC87600";

    password = 16France;

    updated = "2016-02-09 08:46:34 +0000";

    "user-token" = "52323D3B-6FE0-6C14-FF2B-8749379F3D00";

}

Server reported an error: FAULT = '3024' [Unable to update user account due to error: Can not get user schema] <Unable to update user account due to error: Can not get user schema> 

Eric,

Could you please attach a screenshot from Backendless console showing User Properties? It would be on the Users > User Properties screen.

Regards,
Mark

Hi Eric,

From your code I cannot understand - user is logged in or not. If user is not logged in, then self.backendless.userService.currentUser == nil, and any action with it will be unsuccessful.

I try to reproduce your scenario with my code:

    func updateCurrentUserPropsSync() {
        
        Types.tryblock({ () -> Void in
            
            self.backendless.userService.login("james.bond@mi6.co.uk", password: "iAmWatchingU")
            let currentUser = self.backendless.userService.currentUser
            print("User has been logged in (SYNC): \(currentUser)")
            
            let properties = [
                "name" : "Agent 007"
            ]
            
            currentUser.updateProperties( properties )
            let updatedUser = self.backendless.userService.update(currentUser)
            print("User updated (SYNC): \(updatedUser)")
            
            },
            
            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception)" )
        })
    }


This code works fine for me, there is a log :
http://support.backendless.com/public/attachments/de55751459b5007fede0a2b382f7b94e.png</img>

and result in application console:
http://support.backendless.com/public/attachments/1745f733f91316aab67e9589947f2609.png</img>

So, I cannot reproduce your scenario.
Please, try my function in your app, and let me know how it goes.

Regads,
Slava