Read - SrejonKhan/FirebaseRestClient GitHub Wiki
new RealtimeDatabase().Read<T>();
new RealtimeDatabase().ReadValue();
new RealtimeDatabase().ReadKeyValuePairs<User>();
new RealtimeDatabase().ReadKeyValuePairs();
new RealtimeDatabase().RawRead();
new RealtimeDatabase().RawRead(true);
For reading value of a child, which doesn't contain a collection, use Read<T>()
or ReadValue()
Read value and try to convert to the desired type. Throws error, if the value can not be converted to the desired type.
new RealtimeDatabase().Read<T>();
firebase.Child("user/999").Read<User>().OnSuccess(res =>
{
Debug.Log(res.Id);
});
Use when value is known and converted from the JSON. Let's say we have this dataset-
"users" : {
"-Macz8d4Qy7m3yaX4BgA" : {
"hates" : "Sushi",
"id" : 13,
"likes" : "Pizza",
"username" : "ghopper99"
},
"-Macz8gMD3SCmnv5LAoj" : {
"hates" : "Black Pudding",
"id" : 829,
"likes" : "Tacos",
"username" : "srejon79"
}
}
Now, we would like to read -Macz8gMD3SCmnv5LAoj
as User
object. So, we will make a reference to the path users/-Macz8gMD3SCmnv5LAoj
and call Read<T>
. Here, <T>
would be User
class as it matches the JSON Object structure.
So, it stands - new RealtimeDatabase().Child("users/-Macz8gMD3SCmnv5LAoj").Read<User>()
. If we use OnSuccess()
as method chaining for response-
var firebase = new RealtimeDatabase();
firebase.Child("users").Child("-Macz8gMD3SCmnv5LAoj").Read<User>().OnSuccess(res =>
{
Debug.Log(res.Id); //output - 829
}).
OnError(err =>
{
Debug.LogError(err.Message);
});
//User Class
[System.Serializable]
public class User
{
public int id;
public string username;
public string likes;
public string hates;
public User() { }
}
Please remember, it throws an error if the specified type cannot be parsed from the referenced path.
ReadValue()
either returns the value in any primitive datatypes or in JSON String if the value is a JSON Object.
new RealtimeDatabase().ReadValue();
firebase.Child("notices/51").ReadValue().OnSuccess(res =>
{
Debug.Log(res);
});
Use when value is defined as a primitive type or JSON Object is unknown. Let's say we have this dataset -
"scores": {
"alovelace": 126,
"ghopper": 65,
"eclarke": 576
}
Now, we would like to get the score of eclarke
. The best way to get the value is using ReadValue()
. It will return the score in int
as it's defined in int
. The code snippet will seem like after referencing and method chaining -
var firebase = new RealtimeDatabase();
firebase.Child("scores/eclarke").ReadValue().OnSuccess(res =>
{
Debug.Log(res); //output - 576
}).
OnError(err =>
{
Debug.LogError(err.Message);
});
Remember, if we reference somewhere, where it does not return any primitive datatypes, rather returns a JSON Object or a Collection, the response would be a string, where the value is JSON string of that reference.
For retrieving a collections, in Dictionary<string, T>
format, use ReadKeyValuePairs()
or ReadKeyValuePairs<T>()
. Both function does same in operation, use as it fits the requirement.
Retrieve collection in Dictionary <string, object>
format. Value is object, it can be any primitive types or fsData. fsData can contain a dictionary or it can be converted to JSON string.
new RealtimeDatabase().ReadKeyValuePairs();
firebase.Child("users").ReadKeyValuePairs().OnSuccess(res =>
{
foreach (var item in res)
Debug.Log($"Key: {item.Key} - Value: {item.Value}\n");
});
Let's say we have this kind of structured data -
"scores": {
"alovelace": 126,
"ghopper": 65,
"eclarke": 576
}
In that case, if the main requirement is to read all scores as a collection, then ReadKeyValuePairs()
is the right choice. It will return the whole scores collection as a Dictionary <string, int>
, where the key is the name of user, value is the score.
Retrieve collection in Dictionary <string, T>
format. Key as string, value as specified type. Returns nothing if failed to deserialize.
new RealtimeDatabase().ReadKeyValuePairs<T>();
firebase.Child("users").ReadKeyValuePairs<User>().OnSuccess(res =>
{
//Returns response in Dictionary<string,User> (Dictionary<string,T>)
//item.value.id is a property of user class
foreach (var item in res)
Debug.Log($"Key: {item.Key} - Value: {item.Value.id}\n");
});
Let's say we have this kind of structured data -
"users" : {
"-Macz8d4Qy7m3yaX4BgA" : {
"hates" : "Sushi",
"id" : 13,
"likes" : "Pizza",
"username" : "ghopper99"
},
"-Macz8gMD3SCmnv5LAoj" : {
"hates" : "Black Pudding",
"id" : 829,
"likes" : "Tacos",
"username" : "srejon79"
}
}
In that case, if the main requirement is to read all users as a collection, then ReadKeyValuePairs<T>()
is the right choice. Where <T>
will be User class type.
It will return the whole users collection as a Dictionary <string, Users>
, where the key is the UID of user, value is the user object.
Please note, it will return null, if the collection cannot be parsed to the specified type.
For retrieving specified reference/child as raw JSON String, use RawRead()
. RawRead()
comes with one handy feature called shallow.
new RealtimeDatabase().RawRead();
new RealtimeDatabase().RawRead(true); //shallow read
firebase.Child("product").Child("orange").RawRead().OnSuccess(res =>
{
//Returns response in Json string
Debug.Log(res);
});
//Shallow
firebase.Child("users").RawRead(true).OnSuccess(res =>
{
Debug.Log(res);
});
When retrieving a reference as JSON is the main objective, use RawRead()
. Let's say if we want to read a specific User's data in JSON format, we can use RawRead()
.
Or, imagine we are reading a large dataset, where inner data is not important, we can use shallow read. Because in Shallow read, if any location value is a primitive type, it will simply return the value. If the value is a JSON Object, it will return true
. It saves a good amount of bandwidth.
Let's say we have this dataset -
"users" : {
"totalCount" : 14,
"-Macz8d4Qy7m3yaX4BgA" : {
"hates" : "Sushi",
"id" : 13,
"likes" : "Pizza",
"username" : "ghopper99"
},
"-Macz8gMD3SCmnv5LAoj" : {
"hates" : "Black Pudding",
"id" : 829,
"likes" : "Tacos",
"username" : "srejon79"
}
}
For the following dataset, if we shallow read it, the result would be -
"users" : {
"totalCount" : 14,
"-Macz8d4Qy7m3yaX4BgA" : true,
"-Macz8gMD3SCmnv5LAoj" : true
}
As totalCount
is a primitive type, it simply returns its own value. And, for each user's JSON Object, it returns true
indicating a JSON Object.
If you would like to query/filter data, or limit query, please head towards Filter page to learn more.