Un-Caching URL results

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Un-Caching URL results

Post by cusingerBUSCw5N » Wed Dec 12, 2018 1:12 am

Hi. I am creating an app for disasters, and need to have functionality for when there is no wifi. Specifically, if my app tries to send a message and there is no wifi, I want it to continue to try every x seconds until it succeeds. Then I want the app to put the date and time it succeeded into a field (or save the info in a txt file) and then close the app.

When I test this (turning wifi on and off), it seems to cache the results of the first url call. I included code that unloads the last url cache, but I don't think Livecode counts the unsuccessful url calls in its cache.

Here is the code:

Code: Select all

 put 1 into tnum
 repeat until tresult = "Updated and emailed to your employer."
    //I added the next two lines to see if it would solve the cache issues, but it didn't
    put cachedURLs() into tcache
    unload URL the last line of cachedURLs()
   put url(turl) into tresult

   if tresult = "Updated and emailed to your employer." then
      set  the label of button "reported" to  "Sent Date/time: " & the date & the time
      set the vis of button "reported" to true
      shutdownrequest
   else
      answer tresult  //testing to make sure it is still sending the request
      if tnum is 1 then
      answer "The internet is not available. While this app is open, we will continue to send your message every 5 seconds."
  put 2 into tnum  //so the message isn't repeated
end if
end if
     wait 5 secs 
end repeat
So now if wifi is initially off, it perpetually reports that tresult is empty, even after I have turned the wifi back on.

Is there some way I can get the url(turl) to REALLY try each time, rather than just repeat its first result? Or is there a better way to accomplish my goal?

I should say that I am testing this on Windows, but need it to actually work on IOS and Android. It is possible that the caching isn't an issue on a mobile device. I don't know how all of this works...

Thanks!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Un-Caching URL results

Post by jacque » Wed Dec 12, 2018 9:47 pm

The cachedURLs only apply to URLs downloaded with the "load" command, so there won't be anything cached in this case. The command isn't compatible with mobile anyway, so not much use here.

Since the result reports empty on failure, see what is in "it" as well, and let us know what it says. You may be getting a valid server response that contains an error message.

Code: Select all

answer tresult &cr& it
Finally, the handler as written will block all other processes while it remains in the repeat loop and during that time the whole computer can slow down or even lock up. LC will use almost all available CPU resources. Instead of a repeat loop, use "send" to trigger the repeated URL request at 5 minute intervals. Check the pendingMessages function to see if an earlier request is in the queue, and only send a new request if there is no current one pending.

It may be that the tightly running loop is preventing tsNet from actually managing the download correctly. Changing the script to use "send" may fix things.

Last resort: If I remember right, a URL request will remain in the queue until it gets a response. During that time it may appear that the URL is being cached, but actually it's the original request waiting for the server. According to the dictionary there is no default timeout, but you can set one of several to control how long the request waits: DNS timeout, request timeout, or connection timeout. See tsNetSetTimeouts in the dictionary. But you shouldn't need to bother with this if your script and URL are set up correctly.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Un-Caching URL results

Post by cusingerBUSCw5N » Thu Dec 13, 2018 8:33 pm

Thanks for the input. I re-did it and it works! However, I am wondering if the pendingmessages survives after closing the app, or do you have to save them in a txt file and then when the app is opened again, access the txt file?

If it doesn't save it, then I think the code needs to be:

Code: Select all

on shutdown
   --set default folder to writeable directory on mobile
   set the defaultFolder to specialFolderPath("Documents")
   put the pendingMessages into tpending
   put tpending into URL ("file:mobilepending.txt")
   pass shutdown
end shutdown

///and then on openstack:
  if there is a file (specialFolderPath("documents") & slash & "mobilepending.txt")  then
      put url ("file:" & specialFolderPath("documents") & slash & "mobilepending.txt") into tmessage
      repeat for each line templ in  tmessage
         send templ to me in 5 seconds
      end repeat
     end if

Thanks...

For documentation, I have put the code that loops my request below:

Code: Select all

//in the stackscript
  on sendlink
    put field "delayed" of card "disaster" into tlink
    if tlink is empty then
      cancelmessage sendlink
    else
       put tlink into turl
       put url(turl) into tresult
       if tresult  is not empty then
          put "Message sent "& the date & the time into twhat
          set the label of button "reported" of card "disaster" to twhat
      set the width of button "reported" of card "disaster" to the formattedwidth of button "reported" of card "disaster"
      put (the width of this stack - the width of button "reported" of card "disaster")/2 into tleft
      set the left of button "reported" of card "disaster" to tleft
      set the top of button "reported" of card "disaster" to the top of field "message2" of card "disaster" +10
          set the vis of button "reported" of card "disaster" to true
          put empty into field "delayed" of card "disaster"
        cancelmessage sendlink
       else
          send "sendlink" to me in  60 seconds
       end if
       end if
end sendlink
on cancelmessage myMex
 repeat for each line templ in the pendingmessages
  if item 3 of templ is myMex then    
   cancel  item 1 of templ 
  end if
 end repeat
end cancelmessage

/// in the button:

 if  tresult = "Updated" then
answer tresult
   else
      answer "Your message could not be sent. We will re-try every minute."
local sTime
  put 0 into sTime
  send "sendlink" to me in  60 seconds
     put turl into field "delayed"
   end if

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Un-Caching URL results

Post by jacque » Thu Dec 13, 2018 9:27 pm

Everything is lost after shutdown, the pendingMessages are wiped and the internet connection is closed. If you need to store the last URL for resend after shutdown then you'd need to save it to disk, but I don't think I'd repeat all the pending messages if there are duplicates. I'd just send a single unique one when the app restarts and then let your scripts do the resending. Note that leaving the app on Android doesn't always mean it's shut down, it can be idling in the background until Android needs the memory.

You may want to check that the disaster is still current before resending URLs (it could be weeks later when the app restarts,) though I'm not sure how you'd do that or whether it's even a consideration.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”