Using js setInterval on Android vs iOS - react-native

In my application I want to run periodic task. For this I'm using setInterval It's important to notice that the app doesn't have to be in foreground state for the task to run.
On iOS, I use significant location changes to wake-up the application, check some conditions and then start the periodic task this works fine.
On Android, I'm facing some problems with the setInterval. I'm using foreground service,
and when some conditions are met, I want to execute the periodic task, however it never starts. What I've noticed is that if and only if the app is in the foreground state the setInterval actually works. Even if I press the home button and put the app paused state, setInterval stops working.
What I understand from here, is that on Android setInterval requires the UI thread to run but on iOS it doesn't. Is my conclusion is true? Are there any under the hood differences between the setInterval implementations on iOS and Android.

Related

How can I run setInterval in background in expo using expo-task-manager

I am developing one app using react-native and expo in which I have one stopwatch where I am using setInterval to update stopwatch timer its working fine in foreground but when I minimize the app the timer stop working.
After reading lot of documents I found that using expo-task-manager we can run things in background but I don't know how to use it with setInterval.
Thank you in advance
Unfortunately setInterval can not run when app is in back ground, you can do it with AppState listeners, so when your app goes in background and come in foreground calculate the time difference and use setState/useState methods.. to maintain a state..

React Native lifecycle and restarts

Firstly, apologies for the slightly open ended questions but I can't find the info I'm looking for in other questions.
I'm trying to understand the lifecycle of a RN app on both iOS and Android. I understand the app bootstraps when you first start it and stays running while the phone is alive, but what happens when the user switches to a different app and comes back, or their screen times out then they switch it back on? It would be really annoying if the app restarted just because they briefly switched to check their email.
My specific use case (not particularly important to this generic question but included for context) is that I'm trying to build a game with socket.io connections and I'm wondering if I can hook into events to see if the app has been in the background or if I even need to. I have found a way of forcing a restart which may be necessary at some points, but I'd rather just try to reconnect things that have disconnected if I can find out when that happens.
Any push in the right direction would be appreciated.
The app doesn't restart when it goes in the background as you describe. The app keeps its state and the user sees the last screen they visited.
You should have a look at react native's AppState
https://facebook.github.io/react-native/docs/appstate
Using AppState you can addEventListeners that capture the change of the app's state like when going to background.
Of course there are also some problems here...
You can't capture the "kill "event. You can only detect if the app is sent to the background but unfortunately you can't detect when the user chooses to "kill" the app
You can't run any code while your app is in the background. This might be serious in your case but you should evaluate it. For example if you have a timer and you sent the app to the background then the timer stops.

Expo.io Always On Background Location Tracking with watchPositionAsync?

Do compiled Expo.io React Native applications support "Always On" background location tracking, even when the app is not in use?
Expo just implemented Background Location
Instead of using the watchPositionAsync method, you will have to utilize TaskManager.defineTask method to register the background task out outside of the React event loop. Then, start the task with Location.startLocationUpdatesAsync.
Short answer is no. Quoting the "Why not expo" page:
Expo apps don’t support background code execution (running code when
the app is not foregrounded or the device is sleeping). This means you
cannot use background geolocation, play audio in the background,
handle push notifications in the background, and more. This is a work
in progress.
You will need to detach your app and add this behavior yourself, still doing so you lose some of the expo capabilities.

iBeacon entry.exit callback when app is force killed by swipe up iOS

I read in many places that if user swipes up and kills an app, that app will not be able to receive location updates. I get didEnterRegion and didExitRegion callbacks are received when my app is not killed and is in background. But if I swipe kill app, app doesn't get those callbacks. My question here is,
Is it that app will stop getting callbacks ever? i.e., even if the app is launched again and is in background it does not get the callbacks
I read about startMonitoringSignificantLocationChanges and background app refresh when used together, I can get callback even when app is killed by swipe. I tried this and could not get it working. Is this the expected behavior?
Will startMonitoringSignificantLocationChanges and background app refresh together when used, I get region entry/exit callbacks after rebooting device?
This is not expected behavior. You are supposed to get didEnterRegion and didExitRegion callbacks even after killing an app in the task switcher. If you are not seeing this, something may be wrong with your app or testing methodology. Posting code may be helpful.
A few caveats and tips:
Make sure your app has obtained backround permission with locationManager.requestAlwaysAuthorization(). Without it, it cannot detect at all in the background.
Detection in the killed state did not work on iOS 7.0.x. It started working as of 7.1+.
Make sure you wait long enough to get detections. In some app states, it can take up to 15 minutes.
Before killing your app in testing, check logs to ensure you know what CoreLocation thinks is your current region state. If you turn off a beacon then kill your app, CoreLocation may not have had time to realize the beacon disappeared, thinking it is still inside the region. If you then turn on the beacon, you will not get a new entry event because it thinks you are already inside.

Is there a way to determine whether a Windows Store app is suspended?

I'm building a Windows Store app and it pops up toast notifications from time to time. I also have an animation that plays to show when something has updated. Both of these happen at the same time.
What I would like is to not show the toast when the app is running.
So, is there a nice easy way to determine this or do I have to manually track the state via the suspending/resuming events?
Edited info:
The solution has a background task project which goes off, gets the data, then decides if anything has changed that the user needs to know about.
If so, it creates a toast, updates the tile badge, and plays an animation to fade in the new data.
The issue is that I don't want to show the toast and update the tile badge if the user has the app full screen. Similarly, playing the animation isn't needed until the app is resumed (that's the easy part though).
I realize I could solve it by having one timer that works when the app is running, and a separate background task for when it's suspended but that seems like overkill in this case.
The simple answer here is that if your app is suspended, your code won't be running.
If you want to pop up toasts when your app is suspended, you'll either need to use the WPNS or a background task to track changes.