Simply getting my app to do a background task

Discussion in 'iOS Programming' started by JGoose, Aug 20, 2010.

  1. JGoose macrumors member

    Feb 12, 2010
    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] 
         [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?
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    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?
  3. robbieduncan Moderator emeritus


    Jul 24, 2002
  4. JGoose thread starter macrumors member

    Feb 12, 2010
    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.
  5. JGoose thread starter macrumors member

    Feb 12, 2010

    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:?
  6. ianray macrumors 6502

    Jun 22, 2010
    The code you posted calls beginBackgroundTaskWithExpirationHandler, and then immediately calls endBackgroundTask. Presumably, the code you posted is not the actual code you are running?

Share This Page