我试图找出如何检查特定密钥是否在我的Firebase数据库中。我正在使用Xamarin和Xamarin.Firebase.Database nuget包。由于某些原因,此包未在Xamarin Compenents页面上列出,就像其他绑定一样,因此我找不到特定于此包的示例。
我想检查是否已经使用了用户名。
编辑:根据当前答案下面的新代码。
//Database instance
DatabaseReference oRoot = FirebaseDatabase.Instance.Reference.Root;
DatabaseReference oUsernamesRef = oRoot.Child("usernames");
//Username isn't taken
var userListener = new UserValueEventListener((sender, e) =>
{
bool result = (e as UserEventArgs).value;
if (result)
{
lstErrors.Add("The username you selected already exists. Please choose a different one.");
txtUsername.Background = GetDrawable(Resource.Drawable.edittext_modified_states_error);
}
}, sUsername);
oUsernamesRef.OrderByChild("username").EqualTo(sUsername).AddListenerForSingleValueEvent(userListener);
oUsernamesRef.OrderByChild(txtUsername.Text.ToLower()).EqualTo(txtUsername.Text.ToLower()).AddListenerForSingleValueEvent(oListener);
监听器:
public class UserValueEventListener : Java.Lang.Object, IValueEventListener
{
EventHandler OnChange;
string Username;
public UserValueEventListener(EventHandler OnChange, string Username)
{
this.OnChange = OnChange;
this.Username = Username;
}
public void OnCancelled(DatabaseError error)
{
//
}
public void OnDataChange(DataSnapshot snapshot)
{
throw new Exception("here.");
if (OnChange != null && snapshot.Value != null && snapshot.HasChild(Username))
{
OnChange.Invoke(this, new UserEventArgs(true));
}
}
}
事件
public class UserEventArgs : EventArgs
{
public UserEventArgs(bool value)
{
this.value = value;
}
public bool value { get; set; }
}
有没有人能够使用这个包,让这个工作?任何帮助表示赞赏。
让我们假设你有一个用Java(或基于Xamarin.Android的C#)定义的POJO
,名为User
,它包含Name
和Email
属性,而Name
属性是一个唯一的密钥。
调用以下内容将两个用户Bar
和Foo
添加到usernames
键:
var userNamesRef = FirebaseDatabase.GetInstance(firebaseURL).GetReference(User.UserNameRoot);
var userSendToFirebase = new User("Bar", "[email protected]");
var userRef = userNamesRef.Child(userSendToFirebase.Name);
userRef.SetValue(userSendToFirebase);
userSendToFirebase = new User("Foo", "[email protected]");
userRef = userNamesRef.Child(userSendToFirebase.Name);
userRef.SetValue(userSendToFirebase);
您可以稍后使用Bar
查询AddListenerForSingleValueEvent
用户,以便一次性读取数据:
var userNamesRef = FirebaseDatabase.GetInstance(firebaseURL).GetReference(User.UserNameRoot);
var userListener = new UserValueEventListener((sender, e) =>
{
var userFromFirebase = (e as UserEventArgs).Value as User;
Log.Debug(App.TAG, $"User Found!: {userFromFirebase.Name} : {userFromFirebase.Email}");
});
userNamesRef.OrderByChild("name").EqualTo("Bar").AddListenerForSingleValueEvent(userListener);
使用的基于IValueEventListener
的监听器是:
public class UserValueEventListener : Java.Lang.Object, IValueEventListener
{
EventHandler OnChange;
public UserValueEventListener(EventHandler OnChange) => this.OnChange = OnChange;
public void OnCancelled(DatabaseError error)
{
Log.Warn(App.TAG, "Failed to read value:", error.ToException());
}
public void OnDataChange(DataSnapshot snapshot)
{
if (OnChange != null && snapshot.Value != null && snapshot.Key == User.UserNameRoot)
{
foreach (DataSnapshot item in snapshot.Children.ToEnumerable())
{
var user = item.GetValue(Java.Lang.Class.FromType(typeof(User))) as User;
OnChange.Invoke(this, new UserEventArgs(user));
}
}
}
}