Backendless Support
 
Answered

Consistency between local and server user properties

Hi,

I'm struggling on how to handle errors when comunicating to the server due to a lack of information on how some things work on Backendless.

Consider I set a Property "score" on a user and update the user object on the server with a value of 100 points.

Then, my user scores another 10 points. I look to the score property in the BackendlessUser object and add 10 to it.

  1. actualScore = mBackendlessUser.getProperty(SCORE_KEY)
  2. mBackendlessUser.setProperty(SCORE_KEY, actualScore + 10);

Then I call update to upload the user object to the server but it fails due to any reason.

Ok, I save those 10 points that never were saved to try another moment.

Then the user leaves, time goes by and the app gets killed.

The user comes back, I call

  1. Backendless.UserService.isValidLogin();

because is ok, then I get my currenUser.

  1. mBackendlessUser = Backendless.UserService.CurrentUser();

The user scores again. I look to the score property and is 110 because it was setted like that before. But because I'm saving the points that never went uploaded what I will like to know, it's not the score of the currentUser in local, but the score of the CurrentUser that are saved in the cloud.

How can I be sure, that my object mBackdelessUser has the same values/properties that the one stored in the cloud? Is there any kind of synchronization doing on the background besides update method like maybe calling currentUser()?

Thank you for your help!

Best Answer
photo

Hi Jose,

Your description of the issue is quite clear. We will discuss this problem with our team and let you know about the solution.

For now, I can suggest you this workaround

Regards Ilya

Leave a Comment

Comments (6)

photo
1

Hi Jose,

Unfortunately, there is no special method for fetching the current user from the server in SDK.

But you can use the next custom function:

  1. void fetchCurrentUser (AsyncCallback callback)
  2. {
  3. boolean validLogin = Backendless.UserService.isValidLogin();
  4. if (validLogin == false) {
  5. callback.handleFault(new BackendlessFault("There is no valid login user"))
  6. }
  7. BackendlessUser localUser = Backendless.UserService.CurrentUser();
  8. Sting userId = localUser.getProperty("objectId")
  9. Backendless.Persistence.of(BackendlessUser).findById( userId, callback )
  10. }

Using:

  1. fetchCurrentUser(new AsyncCallback<BackendlessUser>()
  2. {
  3. @Override
  4. public void handleResponse( BackendlessUser currentUser )
  5. {
  6. }
  7. @Override
  8. public void handleFault( BackendlessFault fault )
  9. {
  10. }
  11. } )

Regards Ilya

photo
1

Ilya, thank you very much for your answer, but your code makes no sense. Maybe it was a quick copy/paste or a mockup. Could you please paste the same code with more detail?

photo
1

Hi Jose,

Could you clarify the following:

when user re-logins to the app, do you need to retrieve the user from the server with however many points it has in there?

Mark

photo
1

Hi Mark,

I managed find my answer doing several tests. The problem basically is either how the doc or the SDK is implemented.

After I login sucessfully my user I kill the app, this is something that can happen to a regular user after a while using other apps.

When I open the app, I execute this code for testing:

  1. // Tests
  2. Backendless.UserService.isValidLogin(new AsyncCallback<Boolean>() {
  3. @Override
  4. public void handleResponse(Boolean response) {
  5. Log.d(TAG, "It's valid");
  6. BackendlessUser aUser = Backendless.UserService.CurrentUser();
  7. if(aUser != null) {
  8. Log.d(TAG, aUser.toString());
  9. } else {
  10. Log.d(TAG, "user was null");
  11. }
  12. }
  13. @Override
  14. public void handleFault(BackendlessFault fault) {
  15. Log.d(TAG, "It's invalid");
  16. }
  17. });

the output of this code is:

  1. D/Main_activity: It's valid
  2. D/Main_activity: user was null

So getting current user returns a null object, but the login is valid.

However in the documentation: https://backendless.com/documentation/users/android/users_get_current_user.htm

says: "If a user is not logged in, the method returns null."

But a user is logged in since that is what isValidLogin said. So the doc is not ok, or when isValidLogin is called, the user should be refreshed automatically inside the structure so it doesn't return a null when CurrentUser() is called.

photo
1

Hi Jose,

Your description of the issue is quite clear. We will discuss this problem with our team and let you know about the solution.

For now, I can suggest you this workaround

Regards Ilya

photo
1

Thank you Ilya.

photo