Backendless Support
 
Answered

update current user after a change from business logic

What is the right way to update the currentUser? Say one of the properties on the user was updated from outside the client, how can the client fetch an updated model?

Best Answer
photo

Hello Gal,

Please, check this code:

  1. BackendlessUser *currentUser = [backendless.userService currentUser];
  2. id <IDataStore> dataStore = [backendless.persistenceService of:[BackendlessUser class]];
  3. BackendlessUser *user = [dataStore findID:currentUser.objectId];
  4. [user setName:@"XXX"];
  5. [dataStore save:user];

Regards, Olga

Leave a Comment

Comments (6)

photo
1

Hi Gal,

It is a good question. There is no special method for fetching the current user from the server in SDK.

We will add some possibility to do this in nearest future.

Now, as a workaround you can make next custom function:

  1. function fetchCurUser() {
  2. var curUserObjectId = Backendless.LocalCache.get("current-user-id");
  3. if( curUserObjectId ) {
  4. return Backendless.Data.of( Backendless.User ).findById( curUserObjectId )
  5. }
  6. return Promise.resolve(null);
  7. }

And then use this function in next way:

  1. fetchCurUser().then(function(curUser) {
  2. ...
  3. })

I am not sure that you use JS on the client side of your app but I think the main thought is clear.

Also, please pay attention that I use Promise in my example. So if you take this code to your project make sure that you do enablePromises.

Regards Ilya

photo
1

Thanks Ilya, two questions:

1. Why do you use Backendless.LocalCache as opposed to Backendless.userService.currentUser?

2. After a successful callback, is it safe to set the currentUser to the model in the response?

photo
1

Hi Gal,

Firstly, please, could you clarify what programming language do you use for your client side?

Regards Ilya

photo
1

I am working in objective c

photo
photo
2

Hello Gal,

Please, check this code:

  1. BackendlessUser *currentUser = [backendless.userService currentUser];
  2. id <IDataStore> dataStore = [backendless.persistenceService of:[BackendlessUser class]];
  3. BackendlessUser *user = [dataStore findID:currentUser.objectId];
  4. [user setName:@"XXX"];
  5. [dataStore save:user];

Regards, Olga

photo
1

Perfect. Thank you guys.

photo