以下是我将新注册用户添加到Firebase数据库的代码:
TransactionResult AddUserToDatabaseTransaction(MutableData mutableData) {
if (mutableData != null)
{
List<object> users = mutableData.Value as List<object>;
if (users == null)
{ users = new List<object>();
}
users = new List<object>();
// Now we add the new score as a new entry that contains the email address and score.
Dictionary<string, object> newUserRegistration = new Dictionary<string, object>();
newUserRegistration["Dateofbirth"] = "22/03/1980";
newUserRegistration["Email"] = auth.CurrentUser.Email;
newUserRegistration["Full Name"] = "cool";
newUserRegistration["LastLoggedIn"] = "27/06/2017";
newUserRegistration["RegistrationDate"] = "26/04/2017";
users.Add(newUserRegistration);
// You must set the Value to indicate data at that location has changed.
mutableData.Value = users;
return TransactionResult.Success(mutableData);
}
else return TransactionResult.Abort();
}
public void AddUserToDatabase() {
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("users");
DatabaseReference usersaddnow = reference.Child(auth.CurrentUser.UserId);
usersaddnow.RunTransaction(AddUserToDatabaseTransaction)
.ContinueWith(task => {
if (task.Exception != null) {
information.text +=task.Exception.ToString();
} else if (task.IsCompleted) {
information.text += " Transaction complete.";
}
}
);
}
以下是数据库的快照:
所以,我正在尝试将用户添加到数据库中。 我从图片中可以看到,我成功添加了它们,但它在用户详细信息之前添加了一个零子项。你能告诉我为什么会这样吗? 我一直试图摆脱这几个小时,我找不到解决方案。
答案 0 :(得分:0)
问题是因为arraylist 它通过它自己的索引设置数据,即0,1等
所以看起来它在统一中默认使用这些索引 我可以访问测试和内部元素 以这种方式管理数据并访问它更容易