如何在Firebase数据库中的不同引用中关联两个不同的子节点?

时间:2018-03-27 11:05:57

标签: android firebase firebase-realtime-database

我对Firebase来说相对较新,我试图通过将用户数据放入两个不同的路径来避免分层数据,就像在documentation中所说的那样。因此,该计划是为每个用户提供两个不同的唯一标识符;一个节点用于他或她的帐户信息,另一个节点用于任务板。因此,每个用户都应该拥有信息的节点和任务板的另一个节点

enter image description here

问题在于我不能多次使用相同的唯一标识符。

此代码段在数据库中创建用户:

private void addUser(final String email, String password, final String lastName, final String firstName) {
    auth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    Log.d(TAG, "createUserWithEmail:success");
                    FirebaseUser user = auth.getCurrentUser();

                    //Create a reference for task board ids
                    taskBoardRef = database.getReference().child("taskboard_ids");

                    String boardId = taskBoardRef.push().getKey();
                    Log.i("Check",boardId);

                    //Attach user object to user reference
                    userRef.push().setValue(new Employee(firstName,lastName,email,boardId));

                    //Attach a default task board to the taskboard
                    taskBoardRef.push().setValue(new TaskBoard("Default"));


                    //Create a new SessionManagement instance
                    //Temporarily store user info
                    SessionManagement sessionManagement = new SessionManagement(getApplicationContext(),auth.getCurrentUser().getEmail());
                    sessionManagement.createLoginSession();

                    //Go to task activity
                    intent = new Intent(getApplicationContext(),TasksActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
}

这是TaskBoard类: TaskBoard.java

public class TaskBoard {
private String taskBoardName;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference ref = database.getReference("users_tasks");
private DatabaseReference refForTaskBoard = database.getReference("user_task_boards");

public TaskBoard(){}
TaskBoard(String name){taskBoardName = name;}
public List<Task> tasks;

public List<Task> getTasks() {
    return tasks;
}
public String getTaskBoardName(){return taskBoardName; }
public HashMap<String, Boolean> addTask(Task task)
{
    HashMap<String, Boolean> result = new HashMap<>();
    result.put(task.getName(),task.isComplete());
    return result;
}

public static class Task {
    private String name;
    private boolean complete;

    public Task(){}
    public Task(String name, boolean complete)
    {
        this.name = name;
        this.complete = complete;
    }

    public String getName() {
        return name;
    }


    public boolean isComplete() {
        return complete;
    }
}
}

1 个答案:

答案 0 :(得分:2)

要解决此问题,请更改以下代码行:

userRef.push().setValue(new Employee(firstName,lastName,email,boardId));
taskBoardRef.push().setValue(new TaskBoard("Default"));

userRef.child(boardId).setValue(new Employee(firstName,lastName,email,boardId));
taskBoardRef.child(boardId).setValue(new TaskBoard("Default"));

使用上述内容,您可以在两个地方使用与boardId方法相同的push()