我想在firebase数据库中创建一个节点,如下所示:
{
"Users" : {
"SktNBDO4pOgS6wNfFIc5lV8p4u73" : {
"Email" : "[email protected]",
"Name" : "Name Surname"
}
}
}
我正在使用的代码是:
user = auth.CurrentUser;
//Init Firebase
dataBase = FirebaseDatabase.Instance.GetReference("Users").Child(user.Uid).Child("Name");
var postData = new UserListItemViewModel
{
Name = "Name Surname",
Email = "[email protected]",
};
dataBase.SetValue(postData.Name);
我要做的是将所有UserListItemViewModel设置到数据库中,例如:
user = auth.CurrentUser;
//Init Firebase
dataBase = FirebaseDatabase.Instance.GetReference("Users").Child(user.Uid).Child("Name");
Dictionary<string, UserListItemViewModel> userData = new Dictionary<string, UserListItemViewModel>
{
{
user.Uid,
new UserListItemViewModel
{
Name = "Name Surname",
Email = "[email protected]",
}
}
};
dataBase.SetValue(userData);
上面的代码的问题是userData
应该是Java.Lang.Object
。
我遵循Java的教程建议后进入此解决方案
Map<string, UserListItemViewModel> userData = new HashMap(user.Uid, new UserListItemViewModel
{
Name = "Name Surname",
Email = "[email protected]",
})`
请帮助吗?
我用FCM做过类似的功能,您可以参考以下代码:
private DatabaseReference mMsgDatabaseReference;
//************************************
var taskSnapShot = (UploadTask.TaskSnapshot)snapshot;
Android.Net.Uri downloadUrl = taskSnapShot.DownloadUrl;
FriendlyMessage msg = new FriendlyMessage(null,mUserName,downloadUrl.ToString());
mMsgDatabaseReference.Push().SetValue(FriendlyMessage.MsgModelToMap(msg));
方法MsgModelToMap
:
public static HashMap MsgModelToMap(FriendlyMessage msg)
{
HashMap map = new HashMap();
map.Put("text", msg.text);
map.Put("name", msg.name);
map.Put("photoUrl", msg.photoUrl);
map.Put("UId", msg.UId);
return map;
}
FriendlyMessage.cs
class FriendlyMessage : Java.Lang.Object, IParcelable
{
public String text;
public String name;
public String photoUrl;
public String UId;
public void setUid(String Id) {
this.UId = Id;
}
public String getUId() {
return UId;
}
private static readonly MyParcelableCreator<FriendlyMessage> _create = new MyParcelableCreator<FriendlyMessage>(GetMessage);
//[ExportField("CREATOR")]
[ExportField("CREATOR")]
public static MyParcelableCreator<FriendlyMessage> Create()
{
return _create;
}
private static FriendlyMessage GetMessage(Parcel parcel)
{
FriendlyMessage msg = new FriendlyMessage();
msg.text = parcel.ReadString();
msg.name = parcel.ReadString();
msg.photoUrl = parcel.ReadString();
msg.UId = parcel.ReadString();
return msg;
}
public FriendlyMessage()
{
}
public FriendlyMessage(String text, String name, String photoUrl)
{
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.UId = Guid.NewGuid().ToString();
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPhotoUrl()
{
return photoUrl;
}
public void setPhotoUrl(String photoUrl)
{
this.photoUrl = photoUrl;
}
public int DescribeContents()
{
throw new NotImplementedException();
}
public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
{
dest.WriteString(text);
dest.WriteString(name);
dest.WriteString(photoUrl);
dest.WriteString(UId);
}
public static HashMap MsgModelToMap(FriendlyMessage msg)
{
HashMap map = new HashMap();
map.Put("text", msg.text);
map.Put("name", msg.name);
map.Put("photoUrl", msg.photoUrl);
map.Put("UId", msg.UId);
return map;
}
public override string ToString()
{
return "name = " + name +" text = " + text + " photoUrl= " + photoUrl+ " UId = " + UId;
}
public static FriendlyMessage MapToMsgModel(DataSnapshot snapShot)
{
FriendlyMessage msg = new FriendlyMessage();
if (snapShot.GetValue(true) == null)
{
return null;
}
msg.UId = snapShot.Key;
msg.text = snapShot.Child("text")?.GetValue(true)?.ToString();
msg.name = snapShot.Child("name")?.GetValue(true)?.ToString();
msg.photoUrl = snapShot.Child("photoUrl")?.GetValue(true)?.ToString();
return msg;
}
public bool Equal_obj(object obj)
{
var message = obj as FriendlyMessage;
return message != null &&
text == message.text &&
name == message.name &&
photoUrl == message.photoUrl &&
UId == message.UId;
}
}