'c# Firesharp - How to update a single value of an object?
I have a c# project and I'm trying to update a single value of an object using FireSharp. However, when I do, it deletes the other objects that are strings.
tldr question:
Is it possible to update only a single field of an object using Firesharp or do I have to include all fields when updating?
In the example on their GitHub, they set all fields with:
var todo = new Todo {
name = "Execute SET",
priority = 2
};
SetResponse response = await _client.SetAsync("todos/set", todo);
Todo result = response.ResultAs<Todo>(); //The response will contain the data written
and then they update the fields with :
var todo = new Todo {
name = "Execute UPDATE!",
priority = 1
};
FirebaseResponse response =await _client.UpdateAsync("todos/set", todo);
Todo todo = response.ResultAs<Todo>(); //The response will contain the data written
I know this is possible in other languages so I feel like this should be possible in FireSharp.
My Case:
for example if I have a class like StudentProfile:
class StudentProfile {
public string Firstname {get; set;}
public string Lastname {get; set;}
public int Age {get; set;}
}
when I uploaded to firebase, I use:
StudentProfile studentProfile = new StudentProfile
{
Firstname = "John",
Lastname = "Doe",
Age = 18
};
client.Set("Students/" + firebaseUserId + "/StudentProfile", studentProfile);
Now let's say I wanted to update the age. First I would get the info:
var result = client.Get("Students/" + firebaseUserId + "/StudentProfile");
StudentProfile student = result.ResultAs<StudentProfile>();
Now I want to update only the age. However, this is where it seems to update the Age
but then delete the other values that are strings
int newAge = student.Age + 1;
StudentProfile updatedStudent = new StudentProfile
{
Age = newAge
};
client.UpdateAsync("Students/" + firebaseUserId + "/StudentProfile, updatedStudent)
Is it possible to update only a single field of an object using Firesharp or do I have to include all fields when updating?
Basically, it's a pain if I have to consistently write out all of the fields every time I want to update something.
Solution 1:[1]
The simplest way is to just expand the path on the left-hand side to the age, and then use SetAsync
:
client.GetAsync(
"Students/" + firebaseUserId + "/StudentProfile/Age",
newAge
)
Solution 2:[2]
You can store your values to update as Dictionary<string,object>
. Then you can even update and push nested objects by defining the path as string.
Your example:
int newAge = student.Age + 1;
Dictionary<string,object> updatedStudent = new Dictionary<string,object>();
updatedStudent.Add("Age",newAge);
client.UpdateAsync("Students/" + firebaseUserId + "/StudentProfile, updatedStudent);
Adding something like this, would push a nested object to the jsontree:
...
updatedStudent.Add("Address/Street","Main Ave. 1");
client.UpdateAsync("Students/" + firebaseUserId + "/StudentProfile, updatedStudent);
Result would be:
{
"Firstname": "John",
"Lastname": "Doe",
"Age": 18,
"Address":{
"Street": "Main Ave. 1"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |
Solution 2 | Christian Figul |