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.

actualScore = mBackendlessUser.getProperty(SCORE_KEY)
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

Backendless.UserService.isValidLogin();

because is ok, then I get my currenUser.

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!

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:

void fetchCurrentUser (AsyncCallback callback)
{

 boolean validLogin = Backendless.UserService.isValidLogin();

 if (validLogin == false) {
 callback.handleFault(new BackendlessFault("There is no valid login user"))
 }

 BackendlessUser localUser = Backendless.UserService.CurrentUser();
 Sting userId = localUser.getProperty("objectId")
 Backendless.Persistence.of(BackendlessUser).findById( userId, callback )

}

Using:

fetchCurrentUser(new AsyncCallback<BackendlessUser>()
{
 @Override
 public void handleResponse( BackendlessUser currentUser )
 {
 
 }
 @Override
 public void handleFault( BackendlessFault fault )
 {
 
 }
} )

Regards Ilya

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?

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

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:

// Tests
Backendless.UserService.isValidLogin(new AsyncCallback<Boolean>() {
 @Override
 public void handleResponse(Boolean response) {
 Log.d(TAG, "It's valid");
 BackendlessUser aUser = Backendless.UserService.CurrentUser();
 if(aUser != null) {
 Log.d(TAG, aUser.toString());
 } else {
 Log.d(TAG, "user was null");
 }
 }
 @Override
 public void handleFault(BackendlessFault fault) {
 Log.d(TAG, "It's invalid");
 }
});

the output of this code is:

D/Main_activity: It's valid
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.

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

Thank you Ilya.