[英]Change the document's unique ID from string to int in firebase realtime using xamarin form
我的問題是我想在 firebase 實時數據庫中將文檔/表的唯一 ID 從字符串更改為 int。
這是它在我的數據庫中的樣子: .
我想看起來像這樣: .
這是我向 firebase 插入數據的代碼:
public async Task<bool> Save(CUSTOMER customer)
{
//var token = await authProvider.CreateUserWithEmailAndPasswordAsync(customer.CusEmail,customer.CusPassword);&& !string.IsNullOrEmpty(token.FirebaseToken);
var token = await authProvider.CreateUserWithEmailAndPasswordAsync(customer.CusEmail, customer.CusPassword);
var data = await firebaseClient.Child(nameof(CUSTOMER)).PostAsync(JsonConvert.SerializeObject(customer));
if (!string.IsNullOrEmpty(data.Key) && !string.IsNullOrEmpty(token.FirebaseToken))
{
return true;
}
return false;
}
當您調用PostAsync
時, Firebase 會為該數據創建一個具有自己唯一 ID 的新子節點。 ID 始終采用您在第一個屏幕截圖中看到的形式,並且無法更改。
要指定您自己的 ID,請在您的客戶端應用程序代碼中生成該 ID,將其作為額外的Child()
調用傳遞給 API,並使用Put
而不是Post
。 例如:
firebaseClient.Child(nameof(CUSTOMER)).Child("4815").PutAsync(JsonConvert.SerializeObject(customer));
如果您要使用的數字基於數據庫中的現有鍵,您將需要使用事務來執行必要的先讀后寫序列。
由於您正在考慮使用數字鍵,我建議您查看最佳實踐:Firebase 中的 Arrays 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.