Backendless Support
 
Solved

Relationships doesn't save on v.4

Hey guys,

I have an issue. I can't to save BackendlessUser with related objects (one to many). But it saving simple fields (strings, numbers). It happened on v.4 after migration, but all working properly before on v.3 with the same code.

I've enabled all rules.

I can't save next fields for BackendlessUser:

  1. user.setProperty(Consts.DEVICES, devices);user.setProperty(Consts.CONTACTS, foundContacts);

Please look at the followed code and screenshots:

  1. /**

    * 1 step

    */

    private void loginUser() {

    String password = NUser.i().password();

    if (!TextUtils.isEmpty(password)) {

    Backendless.UserService.login(password, password, new AsyncCallback<BackendlessUser>() {

    public void handleResponse(BackendlessUser user) {

    updateContacts(user);

    }

    public void handleFault(BackendlessFault fault) {

    if (fault.getCode().equalsIgnoreCase("3003"))

    registerUser();

    else

    NoteMessage.showAlert("login fault", fault.toString());

    }

    }, true);

    } else

    NoteMessage.showAlert("registration", TextUtil.string(R.string.empty_credentials));

    }

Here I retrieve founded contacts (BackendlessUsers) and set to my BackendlessUser with custom field.

  1. /**

    * 2 step

    */

    private void updateContacts(final BackendlessUser user) {

    if (user != null) {

    List<String> phones = Contact.getPhones();

    if (phones != null && !phones.isEmpty()) {

    String result = TextUtil.Intersperse(phones, ",");

    String whereClause = Consts.PHONE_NUMBER + " in (" + result + ")";

    DataQueryBuilder queryBuilder = DataQueryBuilder.create();

    queryBuilder.setWhereClause(whereClause);

    queryBuilder.setPageSize(50);

    Backendless.Data.of(BackendlessUser.class).find(queryBuilder, new AsyncCallback<List<BackendlessUser>>() {

    @Override

    public void handleResponse(List<BackendlessUser> foundContacts) {

    List<BackendlessUser> usersContacts = retrieveContacts(user, foundContacts);

    updateUser(user);

    Logger.log(TAG, "getFriends");

    }

    @Override

    public void handleFault(BackendlessFault fault) {

    NoteMessage.showAlert("find friends fault", fault.toString());

    updateUser(user);

    }

    });

    }

    }

    }

Set other fields to BackendlessUser and save it. But it doesn't save

  1. /**

    * 3 step

    */

    private void updateUser(BackendlessUser user) {

    if (user != null) {

    NUser.i().setImageUrl(null);

    NUser.i().setAlias(null);

    NUser.i().removeDevices();

    updateFields(user);

    Backendless.UserService.update(user, new AsyncCallback<BackendlessUser>() {

    public void handleResponse(BackendlessUser user) {

    // !!! USER DOESN'T SAVED WITH RELATED OBJECTS

    }

    public void handleFault(BackendlessFault fault) {

    NoteMessage.showAlert("update fault", fault.toString());

    }

    });

    }

    }

  1. private List<BackendlessUser> retrieveContacts(BackendlessUser user, List<BackendlessUser> foundContacts) {

    if (foundContacts == null)

    foundContacts = new ArrayList<>();

    Object[] objects = (Object[]) user.getProperty(Consts.CONTACTS);

    if (objects != null && objects.length > 0) {

    BackendlessUser[] contactsArray = (BackendlessUser[]) objects;

    foundContacts.addAll(Arrays.asList(contactsArray));

    }

    if (foundContacts.size() > 0)

    user.setProperty(Consts.CONTACTS, foundContacts);

    return foundContacts;

    }

  1. private static void updateFields(BackendlessUser user) {

    Object[] devicesObjectArray = (Object[]) user.getProperty(Consts.DEVICES);

    List<NDevice> devices = DeviceMap.updateDeviceList(devicesObjectArray);

    user.setProperty(Consts.DEVICES, devices);

    user.setProperty(Consts.PHONE_NUMBER, NUser.i().phoneNumber());

    user.setProperty(Consts.DIGIT_ID, NUser.i().device().getDigitId());

    if (TextUtils.isEmpty(NUser.i().imageUrl()))

    NUser.i().setImageUrl((String) user.getProperty(Consts.IMAGE_URL));

    else

    user.setProperty(Consts.IMAGE_URL, NUser.i().imageUrl());

    if (TextUtils.isEmpty(NUser.i().alias()))

    NUser.i().setAlias((String) user.getProperty(Consts.ALIAS));

    else

    user.setProperty(Consts.ALIAS, NUser.i().alias());

    }

Leave a Comment

Comments (4)

photo
1

Hi Oleg,

The relations are not saved automatically in v4 any more. You should use our new Relation Management API.

photo
1

Hi Sergey, thanks for the link.

  1. Integer result = Backendless.Persistence.of( E ).addRelation(

    E parentObject,

    String relationColumnName,

    Collection<T> children );

Do you have result explanation? I've got different values of result. How to understand what it does mean?

result = 1, 0, 2 etc..

photo
2

It is described in the documentation. See the "Return value" section here: https://backendless.com/docs/android/doc.html#data_add_relation_with_objects_android

photo
1

Thank you, Mark, for your reply!

photo