2014-04-03 34 views

回答

3

在負責排隊任務的類中,有一個ivar來跟蹤活動任務,例如,

// In MySessionWrapper.m 

@interface MySessionWrapper() { 
    NSMutableSet *activeTaskIds; 
} 
@end 

當你排隊一個任務,你將其ID添加到該組:

[activeTaskIds addObject:@([task taskIdentifier])] 

當你得到一個didComplete回調,將刪除ID,如果活動任務數量下降的下方 目標,你增加更多的任務:現在

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error 
{ 
    // other stuff 
    [activeTaskIds removeObject:@([task taskIdentifier])] 

    if ([activeTaskIds count] < NUMBER) { 
     // add more tasks 
    } 
} 

該系統是爲我工作。

+0

這就是我也是這樣做的,對我來說它工作正常 – user361526

+0

粘土橋我是否需要在didComplete __block中添加這塊作品__block UIBackgroundTaskIdentifier background_task; background_task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {[app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; – Xcoder

+0

@Xcoder關閉。你應該在SO問題中提出這個問題,我可以給你一個更完整的答案。它們的格式也更​​好。 :) –

相關問題