Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JGoose

macrumors member
Original poster
Feb 12, 2010
41
0
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:

Code:
- (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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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?
 

JGoose

macrumors member
Original poster
Feb 12, 2010
41
0
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

macrumors member
Original poster
Feb 12, 2010
41
0
Have you done the things mentioned in the iOS Programming guide, in particular:

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

2) Started your background task with 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:?
 

ianray

macrumors 6502
Jun 22, 2010
452
0
@
The code you posted calls beginBackgroundTaskWithExpirationHandler, and then immediately calls endBackgroundTask. Presumably, the code you posted is not the actual code you are running?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.