Long running task in background - background

I made an app, that downloads and parses xml data from server every 7 minutes.
I want it to work in backgound most of the time, so if there are any changes this app could send a local notification.
I read Apple's requirments for long running background tasks, and there is no appropriate category:
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
Any advices?

Create a NSThread, instead of having an infinite loop with sleeps, you'd be better off using an NSTimer. You can initialize it with timerWithTimeInterval:invocation:repeats: and then schedule it on the run loop your background thread.

Related

Keep App Alive in Background IOS7

I have created the application, which track the GPS Location of user at specific time period. This process is run 3 times in background. So, App need to keep alive in background.
To achieve the our requirement, we use the Location manager (GPS) running in the background. So, it will never been killed by OS. Also, we have run the background task thread while App is in background.
This approach working fine on iOS 6 and before and running more than 10 minute in background.
But in iOS 7 Application going to killed after 10 minute.
Please need suggestion for keep the Timer alive in background.
We would appreciate the earliest response. Thank you in advance.
How to keep app running alive in background in IOS 7 without affecting the battery life.
There's no reason for the app to be killed if it has background location tracking functionality in the Info.plist file and doesn't try to abuse the benefits of that permission.
I'm not sure what's your use case for the tracking functionality, but -- together with an assumption that if there's no record from some period, the tracked device didn't change the location -- setting a distance filter would allow to track the location all the time.
That also allows to put a smaller burden on device's battery, because in certain activity types handled by CLLocationManager, the device may put the location service in idle state if it doesn't detect any significant movement.
if you want to keep app active in background and don't want to go to appstore for some reason (for example you are developing something for your company with using enterprise developer program), you should check deferredLocationUpdates(even on devices which doesn't support them, you just get error in your delegate but app will work) and don't call stopLocationUpdates while in background.(if you use this on app for appstore you have to explain why you needed this to apple of course).

For Win8 app how do I schedule tile notifications/updates when on battery

I'm currently scheduling a background task which then queues a number of tile updates.
When the machine is on power, the background task will schedule the next batch of updates. I'm using a Maintenance type background task which does not run when on battery.
Most of the other types of background task types require the app to be pinned to the lock screen.
I need a background task to run periodically, to schedule the tile updates, including when on battery mode. The information being shown becomes out of date quickly, hence my requirement to frequently update the tile.
I also looked at TileUpdater.StartPeriodicUpdate tile updates but that requires a web service somewhere, my code is local in a background task.
It works as I want as a MaintenanceTask when the machine has power.
I'm pretty sure it is possible, I can see other apps updating when on battery mode, without being pinned.
How can I update tiles periodically (say every minute) regardless of being powered or on battery, from a background task?
There are a few ways an app tile can be updated on battery without the app being pinned to the lock screen. Two of the methods require a web presence: push notifications, and periodic notifications (which you mentioned). This MSDN article goes over each of the delivery methods:
http://msdn.microsoft.com/en-us/library/windows/apps/hh779721.aspx
If the content for tile notifications is not dynamic, scheduled tile notifications can be used. On each launch, an app can schedule some fixed number of tile notifications into the distant future. This MSDN article present an example app scheduling out notifications for a week in advance, and using both app launch and a timer background task to continually keep an app tile up to date:
http://msdn.microsoft.com/en-us/library/windows/apps/hh761464.aspx

iOS Running a timer within your code in the background

So I've read that in iOS, all timers will pause when your app is running in the background. I've also read that you can run tasks in the bg using beginBackgroundTaskWithExpirationHandler (like so).
What I am trying to achieve is to call a method once every 3 minutes, and another method a fixed-amount of time before the first one. I have managed to do this within one NSTimer which repeats in a way that lets me do this. It works fine but is obviously disabled (or paused) when the app is in the background - I can only assume because of the reason described above.
Does anyone know if there's a way to run a timer or at least call something after a specific amount of time so I can do this?
Basically if you want to continue running active in the background you have to meet one of the following requirements. From the Apple docs:
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must
request specific permissions to run them in the background without
their being suspended. In iOS, only specific app types are allowed to
run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they
support and use system frameworks to implement the relevant aspects of
those services. Declaring the services lets the system know which
services you use, but in some cases it is the system frameworks that
actually prevent your application from being suspended.
http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24

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.

Windows8: why use local notifications

I started off on the Win8 metro app (javascript) development recently. For notifications, it is clear how the WNS notifications will be useful for creating live tiles.
However the use case for local notifications is not clear to me. I have these two questions:
is it correct to assume local notifications make sense only for apps that would run in the background e.g. when other apps are running or when the system is locked?
if the above is not true, then kindly suggest some examples of when local notifications will be useful.
regards
CGere
Local notifications are useful to update your tile on the start screen that persist after your app was closed/suspended. For example you might want to update the tile when your app closes with some context, perhaps an image from the last level of the game they were on or such.
When your app goes to the background it has a short period of time to suspend after which your app will no longer be running and thus unable o update the tile. You can however create a background task to run on an event/timer to do some work (such as update your tile).