Back

Bug on backend c# code to update preferences

  • 1
  • REST API
Zuperman
17 Jan, 2024, 21:19

If I want to update the users' preferences, but only one of the values... you wipe out all their preferences. This doesn't make sense. You can try to get all preferences, modify the existing one, and then send it all over... but it's not only bad for network traffic but also horrible for multithreaded environments. You may be updating the whole preferences several times and stepping on each thread ending up with corrupted data.

In addition, you make a mistake and you wipe out all the preferences just trying to modify one key. My point is, when you update preferences it should only update the ones you assign.

Code:

var client = new Client() .SetEndpoint(Utils.AppWriteURL) // Your API Endpoint .SetProject(Utils.AppWriteProjectID) // Your project ID .SetKey(Utils.AppWriteAPI);

Users users = new Users(client);

Dictionary<string, object> dic= new Dictionary<string, object>(); dic["key"] = "value"; await users.UpdatePrefs(user.Id, dic); // this will wipe out all your preferences, all of them, and only place key and value.

TL;DR
The developer is experiencing a bug in their backend C# code that is causing all user preferences to be wiped out when updating just one. This is problematic for network traffic and multithreaded environments. The developer suggests updating only the provided key and having a separate method for replacing all keys if desired. A possible solution is to modify the code to only update the assigned preferences. This will prevent wiping out all preferences and avoid corrupting data. An example code snippet is provided for reference.
Zuperman
17 Jan, 2024, 21:52

Now imagine you have 2 api calls where you have to update a "picture" key and another with a "username" key. If i use that code, one will wipe out the info of the other.

Zuperman
17 Jan, 2024, 21:52

At the moment I'm forced to get the user, get the dictionary, change the value, and send ALL the data back to update.

Zuperman
17 Jan, 2024, 21:53

But even then, if 2 threads try to update picture and username independently, you may end up with one of the values lost because you got all the data and replaced all the data.

Zuperman
17 Jan, 2024, 21:54

My suggestion is that it should update only the provided "key". And maybe have a different method for replace all keys if you want.

Zuperman
17 Jan, 2024, 21:54

or a "replaceAll" parameter

Zuperman
17 Jan, 2024, 21:55

This way you would have 1) Less network traffic 2) Be thread-safer towards updating different preferences 3) Have the option to replace all of them if wanted

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more