How can I keep changes in sync between users?

In my app two or more users can see and manipulate the same data by adding, editing or deleting records.

Say two users, Bob and Alice are both looking at the same page, which contains a list of records fetched from a custom API (thus a data variable). Bob then decides to delete a record off the list, which he can do without leaving the page. At the moment, this sends a DELETE call to the API which removes the record while the variable is also updated using the following formula: WITHOUT_ITEM(data.Records, current).

My question is, how can then Alice (who has been staring at the same page on her device the whole time) automatically see the updated list which no longer has Bob’s record? At the moment, Alice has the option to manually trigger a refresh by tapping on a button within the page.

However, what if Alice decides to edit (or delete) the record that Bob deleted but it still shows up in her UI? The API will still return an error as the record’s id no longer exists, however this would lead to a poor UX, so what would be a better solution?

Hi Juan!

If you look at the default behavior of a data variable on the logic canvas, you can see that the record collection gets re-fetched automatically every 5000 ms (5s). So if Bob deletes the record now (makes a change in the database), it will take up to 5 seconds for Alice to get the change into her UI, depending on how long it has been since the last time the data was fetched in her app.

If you want to support simultaneous editing by several users, you can simply decrease this delay. However, I would only advise to do that for those resources where simultaneous editing is likely, since making a lot of network calls can make the app run a bit heavy.

Hi @Mari,

But wouldn’t that just be making more calls to the API? It doesn’t seem like the most scalable approach (in fact, I had disable that behaviour for that same reason). What if the data hasn’t changed? Correct me if I’m wrong but doing what you suggest would mean calling the API even if there are no changes to the database, which seems like a waste.

I was thinking using something along the lines of a channel subscription (like chat apps), but I wouldn’t know where to begin with. If it helps, I’d be using the Backendless Publishing API.

You then want a back end that pushes changes out to the clients through callbacks or events.

Hey @JOHN_WORSHAM, have you got any ideas as to how that would work?

What i would do actually without needing a custom bsckend would be to update a “changed” record with a Timestamp every time you write changes to the database. And then just poll that one record maybe every 3 seconds to see if the timestamp is different from last time (meaning something changed) and then read back the whole database.if it changed. So every time somebody writes or updates the database they also update the Timestamp record. Just polling that 1 record every few seconds wouldnt cost much api stress at all.

I don’t think that solution will work for me. In my app, two or more users can access and change multiple records from the same list at the same time, so it’s not just one record that I would need to check, but all of them. Doing a check across all records still feels like a waste and not scalable. Also how could that work if a record was deleted? There would be nothing to check in that case.

There’s also the issue that calling the API in this way would very quickly run into backend limits and higher costs, whereas messages would only be sent to a subscribed channel on changes made.

I am really thinking of using subscriptions here, but I have never implemented them before. Also I don’t know if this is even something that AppGyver will let me do, so I was wondering if anyone else had run into this problem and had a go at it or if they are aware this is something even possible.