C# Firebase Restful Post generate unique key

旧城冷巷雨未停 提交于 2020-01-06 14:48:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!