Support Topics Documentation Slack YouTube Blog

Trying to add relations in Backendless. No error but total count of relations added is always 0


(Sandhita Agarwal) #1

Hi guys I am trying to save objects with relations in Backendless via API. I have two classes namely Task and Reminder. A task can be associated with many reminders hence I want a 1:N relationship between the Task table and Reminder table in Backendless. My Task class is as follows:

public class Task {

private List<Reminder> reminders;

public List<Reminder> getReminders() {
    return reminders;
}

public void setReminders(List<Reminder> reminders) {
    this.reminders = reminders;
}

private String ownerId;

public String getOwnerId() {
    return ownerId;
}

public void setOwnerId(String ownerId) {
    this.ownerId = ownerId;
}

@NonNull
public String getObjectId() {
    return objectId;
}

public void setObjectId(@NonNull String objectId) {
    this.objectId = objectId;
}
public Date created;
public Date updated;

public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

public Date getUpdated() {
    return updated;
}

public void setUpdated(Date updated) {
    this.updated = updated;
}



  @PrimaryKey
@NonNull
private String objectId;
    @NonNull
    private String taskTitle;
    @NonNull
    private Date deadline;
@NonNull
    private int isCompleted = 0 ;
@NonNull
    private int isExpired = 0;


@NonNull
public int getIsCompleted() {
    return isCompleted;
}

public void setIsCompleted(@NonNull int isCompleted) {
    this.isCompleted = isCompleted;
}

@NonNull
public int getIsExpired() {
    return isExpired;
}

public void setIsExpired(@NonNull int isExpired) {
    this.isExpired = isExpired;
}

private String mainTask;
@NonNull
private boolean hasMainTask;



public String getTaskTitle() {
    return taskTitle;
}

public void setTaskTitle(String taskTitle) {
    this.taskTitle = taskTitle;
}

public Date getDeadline() {
    return deadline;
}

public void setDeadline(Date deadline) {
    this.deadline = deadline;
}

public String getMainTask() {
    return mainTask;
}

public void setMainTask(String mainTask) {
    this.mainTask = mainTask;
}

public boolean isHasMainTask() {
    return hasMainTask;
}

public void setHasMainTask(boolean hasMainTask) {
    this.hasMainTask = hasMainTask;
}
}

Reminder Class:

public class Reminder {

    private String title;
    private Date time;
    private String objectId;




    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }
}

I am saving the objects and setting up the relation as below:

 public void saveTaskToServer(final Task task) {
        List<Reminder> remindersList = new ArrayList<>();


        remindersList = task.getReminders();
final List<Reminder> savedReminders = new ArrayList<>();

        if(remindersList!=null && remindersList.size()!=0) {
            for
                    (Reminder reminder : remindersList) {

                reminder.setTitle(task.getTaskTitle());
               
                Backendless.Persistence.save(reminder, new AsyncCallback<Reminder>() {
                    @Override
                    public void handleResponse(Reminder response) {
                        savedReminders.add(response);

                                  }

                    @Override
                    public void handleFault(BackendlessFault fault) {

                        Log.i("error saving reminders", fault.toString());
                    }
                });
            }
        }
        Backendless.Persistence.save(task, new AsyncCallback<Task>() {
            @Override
            public void handleResponse(Task response) {

                newTask = response;
                snackbarMessage.postValue("Task Created Successfully.");

            }

            @Override
            public void handleFault(BackendlessFault fault) {

                Log.i("error", fault.getMessage());
            }
        });

        Backendless.Persistence.of(Task.class).addRelation(task, "reminders", savedReminders, new AsyncCallback<Integer>() {

            @Override
            public void handleResponse(Integer response) {
                Log.i("response", "added" + response);
                newTask.setReminders(savedReminders);
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                Log.i("response", "error" + fault.toString());
            }
        });
}

I have tried saving the relation using the tablename:Class:n instead of the parentColumnName. Also tried saving the objectids of the reminders instead of the reminder objects themselves.The task and reminder objects get saved properly in the backendless console in their respective tables but the reminder column in the Task table still remains empty and no relations get added. Relations count in the backendless call in Android Studio also returns 0. Any advice is really appreciated. I have been following this example.


(Oleg Vyalyh) #2

Hi, Sandhita.
Please, look at the basics about how to work with relations.
https://backendless.com/docs/android/data_set_relation_with_objects_android.html
Also, pay attention to “Important” section, where described syntax of how to construct argument “relationColumnName”.

This article could be helpful if you want later to improve you “save” code.
https://backendless.com/how-to-save-an-object-with-all-the-children-in-a-single-backendless-call/


(Sandhita Agarwal) #3

Hi Oleg

Thanks for your reply. I am using backendless in my android app. I have followed all the resources I could find and made a few changes to my code. I am still unable to save the relation. The objects are saved correctly but the parent-child relationship is not working.


(Sandhita Agarwal) #4

I was using the async callbacks. Dont know why I overlooked this before. The save calls were being made before the callbacks could finish hence was getting null values. Thanks for your help. Changed the callbacks to synchronous calls and wrapped them in an async task.