问题
Below is my code that i used to do a POST request to firebase database. What can i do, to allow the subsequence POST request to be in ascending order? I do not want the firebase auto generated unique ID.
Example can be viewed here. enter image description here
public void postEachFirebaseMessage(string dbcolName)
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
user = "UserNameValue",
message = "MessageValue",
});
var request = WebRequest.CreateHttp(@"" + config.BasePath + dbcolName + ".json?auth=" + config.AuthSecret);
request.Method = "POST";
request.ContentType = "application/json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
}
回答1:
I do not want the firebase auto generated unique ID.
If you don't want Firebase to auto-generate a unique key for you, you can generate your own unique key and then PUT data to that full URL. So something like this:
var request = WebRequest.CreateHttp(@"" + config.BasePath + dbcolName + "/yourOwnUniqueKey" + ".json?auth=" + config.AuthSecret);
request.Method = "PUT";
But as said: you will need to ensure yourself that the key is unique. That is a completely valid scenario, but you will have to write the code for generating the unique keys yourself.
If you wonder why Firebase doesn't simply use keys like 0, 1, 2, I recommend reading this blog post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
来源:https://stackoverflow.com/questions/52139789/c-sharp-firebase-restful-post-generate-unique-key