Backendless Support
 
Answered

Update User property

Hello,

I just migrated my project from 3.x to 4 version and made required changes, but some things stopped working.

Probably i missed something, please correct me.

I have User property update operation, which updates related object on User's table.

The relation is one-to-many, so the user has many related objects of some type.

In version 3, when i wanted to add one more related object, i just took the previously retrieved array of objects, added the new one and then performed update as following:

  1. mLoggedInUser.setProperty( "MyGames", updatedRelatedObjectsArray);
  2. Backendless.UserService.update(mLoggedInUser, new AsyncCallback<BackendlessUser>() {
  3. @Override

    public void handleResponse(BackendlessUser backendlessUser)

    {

  4. }
  5. @Override

    public void handleFault(BackendlessFault backendlessFault) {

    }

  6. });

Now with version 4 this section returns strange results: the returned user from the

  1. public void handleResponse(BackendlessUser backendlessUser)

is missing the MyGames property. I see it in the debugger. Same result i see if i get again the logged in user by

  1. BackendlessUser loggedInUser = Backendless.UserService.CurrentUser();

The property is not really deleted from the table, but nothing is actually added.Did i missed some migration operation ?

Regards,Alex.

Leave a Comment

Comments (4)

photo
1

Hi Alex,

Establishing object relations works differently in 4.x Both the parent and the children objects must be in the database at the time when you create a relation. This is described in the "migration" section of the docs:

https://backendless.com/docs/android/doc.html#related_objects

Regards,

Mark

photo
1

Hi Mark,

thank you for the reply.

I read the section, and understand that now i need to perform much more API calls then before.

My relation is as following (simplified form):

Each user in Users table has relation to N Games, each game has relation to 1 Category.

In previous version a could create new Game instance, assign the Category relation to Game in offline, and then add new Game relation to the user in single API call, which performed update to the user.

  1. Game newGame = new Game();
  2. newGame.setCategory(category); //ofline category assign
  3. Backendless.UserService.update(loggedInUser, new AsyncCallback<BackendlessUser>() {
  4. @Override
  5. public void handleResponse(BackendlessUser backendlessUser)
  6. {
  7. }
  8. );

Now, if i understand you correctly, i need 3 API calls for same task.

1. Add new Game instance to the table

2. Assign a Category to the Game (ad relation to game Category)

3. Add User relation to the new Game.

In this way it will take much more time.

Is it correct ?

Regards,

Alex.

photo
1

In some cases there are more APIs, in other cases there are fewer API calls. Here's a detailed summary for the number of calls in 3.x and 4.x:

http://support.backendless.com/topic/relations-management-with-backendless-4#comment-28982

Regards,

Mark

photo
1

Thank you Mark.

I solved it finally by using single Android AsyncTask which performs in the background all three APIs synchronously.

Regards,

Alex.