Simply getting my app to do a background task

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

  1. macrumors member

    Joined:
    Feb 12, 2010
    #1
    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?
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
  4. thread starter macrumors member

    Joined:
    Feb 12, 2010
    #4
    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. thread starter macrumors member

    Joined:
    Feb 12, 2010
    #5

    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. macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    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