Saving with relation

Hello,
I’m migrating to Backendless 4 at the moment but I’m struggling with saving and relations.
I have a table Exercise and a table Favorites. In my table favorites I’m trying to save a relation to my exercise using the follwing code:

private void addToFavorites() {
 isConnected = networkUtil.checkInternetConnectivity();
 if (isConnected) {
 showSnackbar(getResources().getString(R.string.info_adding_exercise_to_favorites));
 floatingActionButton.setClickable(false);



 final Favorites favorites = new Favorites();



 final DataQueryBuilder queryBuilder = DataQueryBuilder.create();



 String whereClause = "objectId='" + exerciseObjectId + "'";
 queryBuilder.setWhereClause(whereClause);



 Backendless.Data.of(Exercise.class).find(queryBuilder, new AsyncCallback<List<Exercise>>() {
 @Override
 public void handleResponse(List<Exercise> response) {
 for (Exercise exercise : response) {
 favorites.setExercise(exercise);
 }



 Backendless.Data.save(favorites, new AsyncCallback<Favorites>() {
 @Override
 public void handleResponse(Favorites response) {
 showSnackbar(getResources().getString(R.string.info_exercise_saved_successfully));
 floatingActionButton.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_fab_star, null));
 favorited = true;
 floatingActionButton.setClickable(true);
 }



 @Override
 public void handleFault(BackendlessFault fault) {
 showSnackbar(fault.getMessage());
 floatingActionButton.setClickable(true);
 }
 });
 }



 @Override
 public void handleFault(BackendlessFault fault) {
 showSnackbar(fault.getMessage());
 }
 });
 } else {
 Toast.makeText(getApplicationContext(), getString(R.string.info_not_connected_to_the_internet), Toast.LENGTH_LONG).show();
 }
}

A new line is added in my Data Browser but the field related to the exercise remains empty. This used to work in Backendless 3.x but can’t find a solution for backendless 4.
I tried using the documentation but no luck (https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android)
Somebody willing to try and help me?
Kind regards

Hi Birger,

Creating relations between objects works differently in 4.x. The difference is described in the doc:
https://backendless.com/docs/android/doc.html#related_objects

In your code when you add exercises to favorites and then save the favorites object, it will not save the relation. Instead use API to create a relation with condition:
https://backendless.com/docs/android/doc.html#data_set_relation_with_condition_android

Regards,
Mark