2012-03-01 27 views
3

我在看Sam的自學iPhone開發,我不明白爲背景提供的示例。非backgrounding代碼:如何使用beginBackgrounTaskWithExpirationHandler選擇性地指定要在後臺執行的操作:

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     count=0; 
     theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 
         target:self 
         selector:@selector(countUp) 
         userInfo:nil 
         repeats:YES]; 
    } 

而且backgrounding版本是:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    counterTask = [[UIApplication sharedApplication] 
       beginBackgroundTaskWithExpirationHandler:^{ 
       // If you're worried about exceeding 10 minutes, handle it here 
       }]; 
    count=0; 
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 
        target:self 
        selector:@selector(countUp) 
        userInfo:nil 
        repeats:YES]; 
} 

我不明白的是,其中的關聯是活動之間的背景和beginBackgroundTaskWithExpirationHandler進行。

在這個例子中的的NSTimer是在後臺運行 - 但如果有一些其他的活動,在後臺執行,以及即假設代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    counterTask = [[UIApplication sharedApplication] 
       beginBackgroundTaskWithExpirationHandler:^{ 
       // If you're worried about exceeding 10 minutes, handle it here 
       }]; 
    count=0; 
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 
        target:self 
        selector:@selector(countUp) 
        userInfo:nil 
        repeats:YES]; 

    x: some other activity to be performed in the background 
    maybe another timer with a difference time interval 
} 

你怎麼會另外指定那x也會在後臺執行?

或者我誤解了它是如何工作的,以及何時調用beginBackgrounTaskWithExpirationHandler它是整個應用程序實際上將在後臺執行?如果是這種情況,那麼爲什麼需要一個任務標識符,因爲您只能啓動一個您的應用程序的任務?

如果情況並非如此,並且可以挑選不同的任務在後臺執行,那麼這是如何實現的?假設在這個例子中X是第二個具有不同間隔和不同過期條件的計時器,如果我希望計時器和x在後臺執行,那麼代碼是什麼樣子?換句話說如果代碼是這樣的:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    counterTask = [[UIApplication sharedApplication] 
       beginBackgroundTaskWithExpirationHandler:^{ 
       // If you're worried about exceeding 10 minutes, handle it here 
       }]; 
    count=0; 
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 
        target:self 
        selector:@selector(countUp) 
        userInfo:nil 
        repeats:YES]; 

    theTimer2=[NSTimer scheduledTimerWithTimeInterval:1.0 
        target:self 
        selector:@selector(aDifferentMethod) 
        userInfo:nil 
        repeats:YES]; 

    theTimer3=[NSTimer scheduledTimerWithTimeInterval:10.0 
        target:self 
        selector:@selector(anotherDifferentMethod) 
        userInfo:nil 
        repeats:YES]; 

} 

如何指定theTimer和theTimer2都在後臺執行,但theTimer3不?

回答

1

或者我誤解了它是如何工作的,以及何時調用beginBackgrounTaskWithExpirationHandler,它將會實際上在後臺執行的整個應用程序?如果是這種情況,那麼爲什麼需要一個任務標識符,因爲您只能啓動一個您的應用程序的任務?

整個應用程序將運行。決定做什麼或不做什麼取決於你。 beginBackgroundTaskWithExpirationHandler:的意義在於它告訴系統你有一些長期的事情要做。它會做一個記錄,然後把你交回一個令牌。當長時間運行的任務完成時,您可以使用該標記呼叫endBackgroundTask:。有可能在那段時間裏,你從來沒有真正進入到背景中。

當您的應用程序將被暫停時,系統會查看您是否有任何待處理的令牌。如果你不這樣做,那麼它會暫停你。如果你這樣做,那麼它可以讓你運行一段時間。系統不關心你如何將這些令牌與工作聯繫起來。您可以爲整個程序使用一個令牌。或者每個對象在需要做某件事時都可以保留自己的標記。問題是,只要你有一個令牌,你通常會被暫停一段時間才能工作。

對於你關於定時器的問題,你通常不應該在後臺做任何計時器工作。你沒有很多時間(最多10分鐘)。你需要做好你的工作並走出去。當您在後臺進行時,您應該暫停(無效)所有定時器。當你恢復時,你將它們設置爲備份。

1

基本上beginBackgroundTaskWithExpirationHandler用於在應用程序轉到Background之前執行一些長時間運行的任務。您應該在應用程序變爲背景之前使用此句柄進行清理。

counterTask = [[UIApplication的sharedApplication] beginBackgroundTaskWithExpirationHandler:^ { //應用程序前進到背景 之前寫入該塊中的代碼將被執行}];

如果您在時間到期之前沒有爲每個任務調用endBackgroundTask,則操作系統會終止該應用程序。

現在運行中在特定時間間隔中的背景的一段代碼,可以使用

[UIApplication的sharedApplication] setKeepAliveTimeout:600處理程序:^ { //這一段代碼將在執行間隔600秒,當應用程序進入背景 }];

在applicationWillResignActive中寫上面的代碼,當應用程序到達前臺時, [[UIApplication sharedApplication] clearKeepAliveTimeout];

在applicationDidBecomeActive中調用此函數。

當應用程序轉到後臺時,除setKeepAliveTimeout中寫入的代碼外,不會執行任何代碼。

相關問題