Page 1 of 1

mergBgTask executing code continually?

Posted: Wed Feb 28, 2024 6:52 pm
by Aduro91
Hi All,

I'd like to use mergBgTask to execute code continually while the app is in the background.

I want the app to continually check a LiveCloud table to see if something is in the relevant table reference, and then send a notification if there is.

I've seen from years ago, someone asking about something similar, and they were told this: "The idea behind background tasks is to complete a take you started while the app was in the foreground. You can't start a new task while in the background." https://stackoverflow.com/questions/298 ... 0#78076550

I'm not sure what to conclude from this and if someone knows whether code cannot be continually running in the background please let me know.

My code would be something like this:

Code: Select all

on UIApplicationWillResignActiveNotification
      mergBgTaskStart
      do checklivecloud
end UIApplicationWillResignActiveNotification

on checklivecloud
      (check for something in table reference of Livecloud table, if it is blah blah notification etc)
      send dolivecloud to me in 10 seconds
end checklivecloud
If the issue is in the 'send to me' could I instead have the code include a repeat that checks and then waits 10 seconds and checks again until UIApplicationDidBecomeActiveNotification or something?

Re: mergBgTask executing code continually?

Posted: Thu Feb 29, 2024 1:30 am
by stam
Do you mean something like suspend or suspendStack? (mergBgTaskStart is iOS only).

If I understood correctly (and forgive me if I haven't) perhaps something like this?

Code: Select all

on suspend
   checkLiveCloud
end suspend
This should run every time the app goes to background. Handlers for resume or resumeStack can terminate any handlers running while in background.

Having said that I have not in any way tested this and not sure if this will actually continuously run in background when it triggers a handler that recursively calls itself with 'send in time'.

Re: mergBgTask executing code continually?

Posted: Thu Feb 29, 2024 12:37 pm
by Aduro91
Thanks Stam, I'll test this and see :D

Although just to note I am working with iOS! I'll start testing things, I just thought since the creator of MergBgTask told someone they couldn't quite do something very similar, that maybe it wasn't worth trying. But I'll test and see.

Re: mergBgTask executing code continually?

Posted: Wed Mar 06, 2024 10:29 pm
by Aduro91
Running continually in the background on iOS doesn't seem to be working.

When I put the app into the background for 30 seconds or so and change the part of the LiveCloud table to 'report', a notification will come through. However any more time and it doesn't work.

Any ideas?

My code:

Code: Select all

local tTable, tRecordIDs, tTarget, tOutputA
on openStack
   mergNotify "UIApplicationWillResignActiveNotification"
   mergNotify "UIApplicationDidBecomeActiveNotification"
end openStack

on UIApplicationWillResignActiveNotification
   mergBgTaskStart
   do checklivecloud
end UIApplicationWillResignActiveNotification

on checklivecloud
   repeat
      put "d6fbbbc6-e811-49d3-8fab-a9104c46f2b5" into tRecordID
      put "Registration" into tTable
      put "cloud" into tTarget
      put cdb_read(tTable,tRecordID,tTarget) into tRecordA
      if tRecordA[tRecordID]["isthereareport"] is "report" then
         mobileCreateLocalNotification "Notification Alert Text", "Go", "Text string sent to the app.", the seconds + 1, true, "1"
         pass checklivecloud
      end if
   end repeat
end checklivecloud
   
on mergBgTaskExpired pBackgroundTaskIdentifier
   mergBgTaskStart
   do checklivecloud
end mergBgTaskExpired