试图在Backendless中添加关系。没有错误,但添加的关系总数始终为0

时间:2018-12-13 06:14:51

标签: android backendless

嗨,我正在尝试通过API在Backendless中保存具有关系的对象。我有两个班,分别是任务和提醒。一个任务可以与许多提醒相关联,因此我想要Backendless中Task表和Reminder表之间的1:N关系。我的Task类如下:

public class Task {


    public Date created;
    public Date updated;
    private List<Reminder> reminders = null;
    private String ownerId;
    @PrimaryKey
    @NonNull
    private String objectId;
    @NonNull
    private String taskTitle;
    @NonNull
    private Date deadline;
    @NonNull
    private int isCompleted = 0;
    @NonNull
    private int isExpired = 0;


    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 List<Reminder> getReminders() {
        return reminders;
    }

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

    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;
    }

    @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;
    }

    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 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;
    }
}

我正在保存对象并按如下所示建立关系:

    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 remidners", fault.toString());
                    }
                });


            }
        }
 Backendless.Persistence.save(task, new AsyncCallback<Task>() {
                @Override
                public void handleResponse(Task response) {

                    newTask = response;
                    Log.i("id is ", newTask.getObjectId());

                    insertTask(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());
            }
        });

    }

我尝试使用tablename:Class:n而不是parentColumnName保存关系。还尝试保存提醒的对象ID而不是提醒对象本身。任务和提醒对象已正确保存在各自表的无后端控制台中,但“任务”表中的提醒列仍然为空,并且未添加任何关系。 Android Studio中的无尾调用中的关系计数也返回0。真的很感谢任何建议。我一直在关注this示例。

1 个答案:

答案 0 :(得分:0)

我的关系没有得到保存,因为我在backendless中使用了异步回调!!我不知道为什么我以前没看到过。由于保存调用是在异步回调完成之前进行的,所以我最终得到了空值。通过使呼叫同步并将它们包装在异步任务中来解决此问题。