"Editing" flag with multiple users accessing the same data

Hello everyone,
I´m building an app where multiple users can access the same data.

I would like that a specific row can be accessed only by one user per time or eventually I would like that the app can display a message to the user like “User X is already editing this row. Do you want to proceed?”

I was thinking to have a recurrent server call that writes an editing flag in the row that the user is editing, so that other users will be aware before accessing the data but I´m not sure if this is a good approach.

Do you tried something similar? Do you have some recommendations or a different idea?
Thanks!

Hi @MattDa

Have you looked at the Anvil Extras messaging module?
Messaging — Anvil Extras documentation (anvil-extras.readthedocs.io)

A possible solution might be that you subscribe to a channel and monitor that channel exactly as you’ve mentioned above.

Hi @Neeeco,
I was not aware of that possibility, it looks interesting! Tahnks for the suggestion!

In any case, if I´m not wrong anvil extras can be used only for non-commercial applications, so in my case it might not be the best option unfortunately.

Anvil extras is released under MIT licence which is fine to use for commercial applications.

MIT License (Expat) Explained in Plain English - TLDRLegal

You must include copyright and licence, but it’s fine to use commercially.

I am no lawyer and this is not legal advice, so consult with a legal practitioner.

Great, thank you very much.

About your idea:
If I will use the channel to post messages from different users they will be accessing different channels I guess, right?
Because each user will have its own instance of the Publisher class. Am I missing something? I would need a unique publisher where different users can communicate but it should be running on a persistent server.

I haven’t implemented this yet, so I can’t be of much more help. I have a similar use case that I was planning on using the messaging module for, but haven’t come round to actually doing it.

1 Like

I added a timestamp column to the document table, and use the technique described here: Realtime UI DB connection - #6 by stefano.menci

I’ve used a “check_out” true false column on rows in a massive app with multiple concurrent users and it was mega effective. Easy to call, easy to read, easy to use.

@socint This is for sure the easiest way. But how did you manage if a user closes the browser without updating the check_out column?

Nice, thanks @stefano.menci. I will for sure use a timer to check periodically if someone is editing.
I will let you know how it goes.

you can use “row_version” instead of “chek_out” flag
each time the row is saved check that “row_version” wasn’t changed and only then save the record and update its “row_version”

You can have a scheduled background task which releases them after a set period using Scheduled tasks. Something like:

@anvil_background_task
def release_checkouts():
   rows = app_tables.the_table.search(checked_out=True)
   for row in rows:
         if #some condition:
            row.update(checked_out=False)

There are hundreds of ways, really.

If you logged the user checking something out, you could use a heartbeat on the client to detect browser abandonment and trigger a release.

So like:


@anvil_background_task
def release_checkouts():
   no_heartbeats = app_tables.users.search(heartbeat=False)
   for user in no_heartbeats:
      email = user[‘email’]
      rows = app_tables.the_table.search(checked_out=True,user=email)
         for row in rows:
            row.update(checked_out=False)

I’m tired but hopefully you catch my drift.

There is stuff in the forum somewhere about heartbeats, I’m sure I’ve seen it.

Managing check in/check out can be very powerful and very messy. You never end adding options.

That’s why I didn’t go that route. I decided to go with the timer and it’s much easier, both for me to do it bug free and for the users to use.

The only problem is that multiple users can open the same document in read only, then they all can start editing it, then the first one that saves some changes triggers the update of the timestamp, which triggers all the other users with the same document open to get a notification that says either “the document was changed by user X, do you want to reload?” if the document was not edited yet, or “the document was changed by user X, do you want to reload and lose your changes?” if the document was edited.

At first I thought this would be a problem, but the users learned to save very often and I had zero complains.

2 Likes

That’s interesting Stefano! I ended up going the other way after UR sessions and came to a non messy pattern. Love that there is so much flexibility with Anvil itself that we can flex as creators.

Thank you everyone for the suggestions!
I will start to build on top of yours ideas adapting to our app. I think the best option for us will be to have a timer on the client updating the row when editing and notifying other users, similar to what suggested by @stefano.menci.

Let’s see how it goes, I will update you on the development.
Thanks again