2011-07-21 187 views
0

大家使用下面的代码后台任务,它工作正常,当iPhone与Xcode的连接,但是当我跑不连接的Xcode的应用程序,那么后台任务将无法正常工作后台任务只运行

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    back=1.0f; 

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; 
    NSRunLoop *runLoop=[NSRunLoop currentRunLoop]; 
    timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeCounter) userInfo:nil repeats:YES]; 
    [runLoop run]; 
    [pool release]; 
} 

请帮助为什么发生这种情况

回答

2

你检查的背景executation的文档?

你应该开始像任务:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task. 

     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html