PDA

View Full Version : Simply getting my app to do a background task




JGoose
Aug 20, 2010, 09:35 AM
I am just trying to test an idea for a background task, but I can't get my test app to do anything in the background.

I have a button to start the task. Here is the code I have:

- (IBAction) startTask: (id) sender

{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^(void){}];

[self startBackgroundTask];

[[UIApplication sharedApplication] endBackgroundTask:bgTask];

NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
}


While I am in the app the code executes, but as soon as I leave the app it stops. I checked the background time remaining as you see above and in the
applicationDidEnterBackground:. The one above returns a huge number because it is still in the app. But the other one only returns 9.898437. But even then the task doesn't run for even 9 seconds. It terminates immediately.


Also I have another question. Will an app log to the the console while it is running in the background? Just in case it doesn't I set it to log (after returning to the foreground) how much progress it made while in the background. It returns no progress made.

It compiles successfully without any warnings. What am I doing wrong? Any suggestions?



dejo
Aug 20, 2010, 09:55 AM
You have two startTask methods, one with parameters and one without? If so, that seems confusing. And if that's the case, can you post the code for startTask?

robbieduncan
Aug 20, 2010, 11:18 AM
Have you done the things mentioned in the iOS Programming guide (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5-SW4), in particular:

1) Declare your background task types with the UIBackgroundModes in your Info.plist

2) Started your background task with beginBackgroundTaskWithExpirationHandler: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:)?

If not please, go and read the guide (and follow it).

JGoose
Aug 20, 2010, 01:41 PM
You have two startTask methods, one with parameters and one without? If so, that seems confusing. And if that's the case, can you post the code for startTask?

oops, they are not the same I just changed the method names because I did not want to disclose my idea (i have method names relevant to the task). they are two different methods.

JGoose
Aug 20, 2010, 01:49 PM
Have you done the things mentioned in the iOS Programming guide (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5-SW4), in particular:

1) Declare your background task types with the UIBackgroundModes in your Info.plist

2) Started your background task with beginBackgroundTaskWithExpirationHandler: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:)?

If not please, go and read the guide (and follow it).


1)The way I understand it, that is only necessary if you use VoIP, or audio, or location in the background.

2)Did you even read my post? I used beginBackgroundTaskWithExpirationHandler: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:)?

ianray
Aug 20, 2010, 05:34 PM
The code you posted calls beginBackgroundTaskWithExpirationHandler, and then immediately calls endBackgroundTask. Presumably, the code you posted is not the actual code you are running?