我正在使用Firebase API for Unity,并使用Post请求将一些数据推送到实时数据库在使用该Post请求后,我需要为新节点创建的ID来做其他事情,但我不确定如何检索该ID。
Firebase.Instance.Post(uri, new Dictionary<string, string>() { { "name", name } },
delegate { Debug.Log("Name has been added successfully!"); //here is where i need the new ID to do something else },
delegate { Debug.Log("Something Wrong! .. Please try again later"); });
岗位职能:
public void Post<T, K>(URI uri, T body, System.Action<K> onSuccess, System.Action<string> onFail)
{
RequestHelper currentRequest = new RequestHelper
{
Uri = uri.Path,
BodyString = JsonConvert.SerializeObject(body),
IgnoreHttpException = true
};
Debug.Log("BODY_post: " + currentRequest.BodyString);
RestClient.Post(currentRequest, (exception, res) => ResolveResponse(exception, res, onSuccess, onFail));
}
void ResolveResponse<T>(RequestException exception, ResponseHelper res, System.Action<T> onSuccess, System.Action<string> onFail)
{
string returnedText = res.Text;
AuthError authError = null;
try
{
authError = JsonConvert.DeserializeObject<AuthError>(returnedText);
}
catch (System.Exception ex)
{
Debug.Log(ex);
}
finally
{
if (authError != null && authError.error != null && authError.error.message != null)
{
onFail(BeautifyMessage(authError.error.message));
}
else if (exception != null && (exception.IsHttpError || exception.IsNetworkError))
{
onFail(BeautifyMessage(exception.Message));
}
else if (typeof(T) == typeof(string))
{
onSuccess((T)(object)returnedText);
}
else
{
onSuccess(JsonConvert.DeserializeObject<T>(returnedText));
}
}
}
它调用restapi的库post函数。
您正在包装Firebase REST API,其中调用POST
返回响应中的密钥:
成功的请求由200 OK HTTP状态代码表示。响应包含POST请求中指定的新数据的子名称。
{ "name": "-INOQPH-aV_psbk3ZXEX" }
因此,您的响应处理程序将需要解析该结果,并将"-IN...."
密钥返回给调用者。