Back

Add and delete one item from array relationship in Appwriter Flutter

  • 0
  • Flutter
tuyen3962
7 Dec, 2023, 03:09

Can i add or remove one item from array over the simple code for update docment?

TL;DR
The user is asking about adding and deleting items from an array in their Appwriter Flutter app, without running into race condition issues. One suggestion is to use a locking mechanism by creating a lock document in a separate collection, performing the add/remove operation on the array, and then removing the lock document. If the lock already exists, the code should retry creating it until it succeeds or reaches a maximum number of retries. To update the array relationship, the user can simply add the ID of the related document to the array. If two parts of the code try to update the count simultaneously, the second execution will fail if the lock
Drake
7 Dec, 2023, 08:22

Same as any other array, it's not possible at the moment.

WhiteWalker72
7 Dec, 2023, 08:56

Would you have any suggestions on how to add/remove to an array without bumping into race conditions?

Drake
7 Dec, 2023, 09:02

Don't use arrays πŸ˜…

WhiteWalker72
7 Dec, 2023, 10:18

So there is basically no way to manage this then? I'm mostly asking this because I have to increase a counter but then only way to do so is to get the document and update it with a higher count which wouldn't work because of race conditions. Would it be possible to use an appwrite function and mark it as sync somehow so it will execute once the previous execution is done?

Drake
7 Dec, 2023, 10:27

For that counter, I would implement a sort of locking mechanism using unique indexes or something like that. For example, let's say I have a likes attribute on a collection. Id create a likesLocks collection. When updating the like, I would

  1. create a document with the same id in the likesLocks collection
  2. Update the like count
  3. Delete the lock document
WhiteWalker72
7 Dec, 2023, 10:36

So what would happen when two parts of the code try to update the like count in this case? The second execution will try to create the lock while the lock already exists? So then the second execution will fail and has to retry until succeeding? I guess that could work πŸ€”

tuyen3962
7 Dec, 2023, 11:00

so when i want to update the array relationship. How can i do for this?

tuyen3962
7 Dec, 2023, 11:01

and when i create new document with having the related collection, is I just add the id of related document to array?

WhiteWalker72
7 Dec, 2023, 11:12

You could do something like Steven suggested:

  1. create a document in a collection called 'locks' by an unique index that's unique for your action for example: 'update-relationship-array'
  2. execute your action: add/remove from the array and update the document with the updated array
  3. remove the lock document Now step 1 could fail because the lock already exists so your code should catch this and try again to create the lock document until either retrying too many times or until the lock is deleted. This way you can add and remove from arrays without bumping into issues with race conditions
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