How to update contents without holding up the user?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

How to update contents without holding up the user?

Post by sritcp » Fri Feb 14, 2020 3:54 pm

I am writing an app which requires it check the AWS server periodically for new content (text or pdf files) and download it. Since the new content is strictly not essential for the user to use the app, the user should be able to go ahead and start using the app while the content is being updated. What are the general guidelines here? How is this best achieved with LC? Can updates be done in the background?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8180
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to update contents without holding up the user?

Post by FourthWorld » Fri Feb 14, 2020 4:04 pm

While "get url" is indeed blocking, the "load url" command is non-blocking, with a callback message to notify you when the download is complete:
https://livecode.com/resources/api/#liv ... cript/load
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to update contents without holding up the user?

Post by sritcp » Fri Feb 14, 2020 4:49 pm

FourthWorld wrote:
Fri Feb 14, 2020 4:04 pm
...the "load url" command is non-blocking, with a callback message to notify you when the download is complete ...
Yes, I noticed that ....

1. I guess one should never use "put URL .... into tTemp" with internet documents because the remote server may hang or be extremely slow, and we wouldn't know. So, use "load URL ... " every time.

2. "Load .... " doesn't work when the access needs to be authenticated (as with AWS), and the content is accessed with LC AWS library commands such as AWSGetObject. I assume these are non-blocking, too?

3. This is my (tentative) procedure:
a. Check periodically a public file that contains a single number -- the update date. If it is later than what is on the device, initiate update.
b. Check authentication and connection by downloading a tiny private file.
c. Download the bucket contents -- name, date modified, size
d. Compare it with the local storage -- make a list of files that are new or modified, i.e., to be downloaded.
e. Download the files.
f. Update local metadata

Step (e) above is likely to be relatively time-consuming; if there are 10-15 pdf files to be downloaded (each 250K size, say), it may take several seconds to minutes, depending on the quality of the internet connection and the point of access in the world. I need to come up with a strategy. Since this problem is faced by every app that refreshes content regularly, I figured there must be a well-tested, generally accepted strategy available.

Regards,
Sri

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8180
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to update contents without holding up the user?

Post by FourthWorld » Fri Feb 14, 2020 6:19 pm

sritcp wrote:
Fri Feb 14, 2020 4:49 pm
FourthWorld wrote:
Fri Feb 14, 2020 4:04 pm
...the "load url" command is non-blocking, with a callback message to notify you when the download is complete ...
Yes, I noticed that ....

1. I guess one should never use "put URL .... into tTemp" with internet documents because the remote server may hang or be extremely slow, and we wouldn't know. So, use "load URL ... " every time.
"Every time" may be too broad. Both blocking and non-blocking are provided for good reason.

I have a couple apps where no meaningful action can be performed by the user until the app finishes obtaining data from the server. In that case, blocking is exactly what I want. If I was using a server where its latency became prohibitive, I would replace it with one that performs acceptably.
2. "Load .... " doesn't work when the access needs to be authenticated (as with AWS), and the content is accessed with LC AWS library commands such as AWSGetObject. I assume these are non-blocking, too?
I haven't yet dived into the AWS lib so I don't know. I would have to refer you to those docs. But given that object stores often hold data of varying sizes, and some may be large, I'd guess a non-blocking option is provided.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to update contents without holding up the user?

Post by sritcp » Fri Feb 14, 2020 8:33 pm

FourthWorld wrote:
Fri Feb 14, 2020 6:19 pm
.... where no meaningful action can be performed by the user until the app finishes obtaining data from the server...
Of course!
If I was using a server where its latency became prohibitive ....
One of the challenges of serving a low-incidence target population is that, you may have a user in a distant part of the world accessing a file on a U.S. server .... Latency is probably the norm.

Here's my sample code. Is this overkill?

Code: Select all

command xCheckForUpdate  -- I prefix my custom handlers with “x” to identify them as such
local tURL,tStatus,tUpdateDate,tLastUpdate

put “https://mysite.com/myfile.txt” into tURL
load URL tURL

repeat with i = 1 to 30
put URLStatus(tURL) into tStatus
if tStatus is “cached” then exit repeat -- successfully loaded
if (tStatus is empty) or (tStatus is “error”)  or (i >= 30) then
  send “xCheckForUpdate”to me in 3600 seconds -- failed, try again in 1 hour
  exit xCheckForUpdate
end if
wait for 100 milliseconds -- give it 3 seconds total
end repeat

put URL tURL into tUpdateDate
unload URL tURL

if tUpdateDate > tLastUpdateDate then  -- tLastUpdate is available, we assume here
send “xUpdateContents” to me in 0 seconds -- initiate the update procedure
send “xCheckForUpdate” to me in 86400 seconds -- check again tomorrow (in case the user doesn’t close the app)
end if
end xCheckForUpdate
Thanks,
Sri

Post Reply

Return to “Internet”