I currently have a firebase project that parses data from the web.
All the data is coming down however I'm wanting to reverse order the results.
I achieve this by doing the following code
public class ExampleItemModel{
public string name;
public string score;
public string level;
public int iconIdx1, iconIdx2, iconIdx3;
}
Then in my fetch data method :-
public void GetDataFromFireBase(string reference, Action<ExampleItemModel[]> onDone){
FirebaseDatabase.DefaultInstance
.GetReference(reference).OrderByChild("score")
.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted) {
// Handle the error...
print("Error: " + task.Exception);
}
else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
var items = snapshot.Value as Dictionary<string, object>;
int count = items.Count;
var results = new ExampleItemModel[count];
int i = 0;
foreach(var item in items){
results[i] = new ExampleItemModel();
//print("items = " + item);
var values = item.Value as Dictionary<string, object>;
if (reference == "scores"){
print("values count =" + values.Count);
foreach(var value in values){
if(value.Key == "name"){ results[i].name = "" + value.Value;}
if(value.Key == "score"){ print("Score = " + value.Value); results[i].score = "" + value.Value;}
if(value.Key == "level"){ results[i].level = "" + value.Value;}
}
i++;
}
onDone(results);
}
});
}
Ive tried the following : -
in firebase rules I've set
".indexOn":"score",
"score": {
".indexOn": ".value"
}
I've also tried to add the following line of code
results = results.OrderByDescending( x => x.score);
onDone(results);
but I get the following error
severity: 'Error' message: 'Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'ExampleItemModel[]' [Assembly-CSharp]' at: '137,14' source: ''
Would anybody please be able to help
thanks