Check for data on remote database every five minutes ios - objective-c

What is the right way to check for data in remote database through http requests in objective c iOS. I am thinking of an nstimer that is called every 5 minutes. The nstimer will trigger a function with a thread in it. Is this the right way? Is this going to work when the app enters the background?
Any help appreciated.

The thread (as like all execution in your program) will pause when entering the background - and if it was waiting on a network response, that response will fail after the app returns to the foreground.
Moreover, you need to explicitly tell iOS when you are beginning a task that you would like to continue in the background (with beginBackgroundTaskWithExpirationHandler: on your UIApplication singleton) and when you have finished that task (with endBackgroundTask:). However, that is only up to a maximum of ten minutes, so I daresay you won't be able to, say, continue your NSTimers in the background. But yes, the method you have described is fine for when the application is in the foreground.

Related

ExternalAccessory streams delegate not always called in background

I am experiencing a bad issue with the EA framework.
Everything is working when the app is in foreground, when the accessory, I open a session, retrieve the streams, set their delegate and schedule the streams on the main run-loop
The accessory is pinging my iPhone every second, and these pings are read with the stream delegate. This ping is a kind of heartbeat : if the iPhone does not received any ping in for seconds, the user is alerted.
This app should work in background, so I have added the corresponding keys in my plist file
When the app run in background, everything work fine for 10-30 minutes, and suddenly, for 7-8 seconds, the stream delegate is not called ( but the ping is sent from the accessory), and then in one second, the delegate is called eight times (for all the pings sent during the last seconds)
It looks like my app is suspended for eight seconds, and then all the events that occured during this interval are queued, and then delivered later. The problem is that, as the application does not received any ping during these 8 seconds, the user is incorrectly alerted
I dont understand this behaviour. Does anybody ecounters the same issue ?
I am considering to poll the stream via a background thread, instead of scheduling them on the main run-loop.
Thanks,

dispatch_async vs beginBackgroundTaskWithExpirationHandler

I need to implement a background process in an iOS app, that performs a job every 60 seconds. I know this can only be done while the app is in focus, but I do want this job to finish running in the background after the app is closed. I will use GCD to dispatch a timer that calls my job asynchronously every 60 seconds using either dispatch_async or beginBackgroundTaskWithExpirationHandler.
I am wondering which method is the best, or if they are essentially the same. Is it okay to use beginBackgroundTaskWithExpirationHandler to execute a job even while the app is in the foreground? Or am I better off trying to cancel the job when the app state changes, and then restart the job as a background task?
-beginBackgroundTaskWithExpirationHandler: doesn't run code on a background thread/queue. It tells the OS that you are going to continue doing work when your app is not active.
The two serve completely different purposes.

Desing pattern for background working app

I have created a web-service app and i want to populate my view controllers according to the response i fetch(via GET) in main thread. But i want to create a scheduled timer which will go and control my server, if there becomes any difference(let's say if the count of an array has changed) i will create a local notification. As far as i read from here and some google results, i cant run my app in background more then ten minutes expect from some special situations(Audio, Vo-IP, GPS).. But i need to control the server at least one per minute.. Can anyone offer some idea-or link please?
EDIT
I will not sell the app in store, just for a local area network. Let's say, from the server i will send some text messages to the users and if a new message comes, the count of messages array will increment, in this situation i will create a notification. I need to keep this 'controlling' routing alive forever, whether in foreground or background. Does GCD give such a solution do anyone have any idea?
Just simply play a mute audio file in loop in the background, OR, ping the user's location in the background. Yes, that will drain the battery a bit, but it's a simple hack for in-home applications. Just remember to enable the background types in your Info.plist!
Note: "[...] I fetch (via GET) in main thread." This is not a good approach. You should never fetch any network resources on the main thread. Why? Because your GUI, which is maintained by the main thread, will become unresponsive whenever a fetch isn't instantaneous. Any lag spike on the network results in a less than desirable user experience.
Answer: Aside from the listed special situations, you can't run background apps. The way I see it:
Don't put the app in the background. (crappy solution)
Try putting another "entity" between the app and the "server". I don't know why you "need to control the server at least one per minute" but perhaps you can delegate this "control" to another process outside the device?
.
iOS app -> some form of proxy server -> server which requires
"babysitting" every minute.

iPhone app sending data while closed

I want to send data to my web server while the app is closed. Is that possible? I've read I can send the position, but I waant to send some id of the phone too.
If it's not possible to have the app running, could I at least communicate with it from my server and then do stuff in the background?
Thanks
When the app goes into the background the applicationDidEnterBackground method on your App Delegate will get called.
In that method you can use the beginBackgroundTaskWithExpirationHandler on UIApplication object to start background processing.
Just realise that you don't get forever to perform tasks in the background. You can find out how long you have left by reading the backgroundTimeRemaining property in UIApplication if you need to know if you're running out of time.
If your processing is short you should be fine, but remember if your processing requires network access then you can't be sure how long that will take.

Exiting application iOS

When my application loads, using the didFinishLaunchingWithOptionsi parse data from the internet to nsarrays. My question is, when the user exists the application by using the 'home' button, and then loads the application again how can the data be re-loaded? (because if the data does not reload - if there are any updates on websites, the new updates will not be seen).
Add an applicationWillEnterForeground method to your app delegate. Load the data there, or start a thread to load it if you like.
You should probably also periodically check for new data even while the app remains open, because the user could go idle for a long time.
As an aside, you shouldn't do anything which might block in applicationDidFinishLaunchingWithOptions. If you are using synchronous NSURLConnection APIs there is a danger the OS might kill your app for taking too long to launch. Best to either use the asynchronous/NSURLConnectionDelegate APIs or do the networking on a background thread and call back to the main thread when you need to update UI (UIKit does NOT like being called from background threads, as it is not thread safe. It might appear to work sometimes, but it will come back to bite you sooner or later).