2014-01-07 54 views
3

我想知道你是否看到這個或可能有一些想法,爲什麼我看到我的代碼中的以下行爲:我有NSURLsession與背景配置。當程序在前臺運行時,我會啓動定期下載任務,並且一切正常。當我模擬backgroundfetch(在xcode中)時,我的任務得到一個空值(儘管請求和會話不爲空)。當然在這種情況下,我的會話委託永遠不會被解僱以執行完成處理程序。如果我模擬後續的後臺提取,它們都會在後面工作。在這一點上,如果我將該應用程序帶到模擬器的前臺,並且模擬了另一個後臺存取,症狀會遍佈全部。我在我的appdelegate類中使用了這段代碼。下載任務是空的第一個請求時,當在後臺IOS IOS

非常感謝您的幫助。

- (NSURLSession *)FlickrSession 
{ 

if(!_FlickrSession) 
{ 
    NSLog(@"setting new FlickrSession"); 
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:FLICKR_SESSION]; 
    configuration.allowsCellularAccess = NO; 

    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     _FlickrSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
     _FlickrSession.sessionDescription = FLICKR_SESSION; 
     NSLog(@" new self is %@", _FlickrSession); 
     NSLog(@"queue in session %@", dispatch_get_current_queue()); 

    }); 
} 
return _FlickrSession; 

} 

-(void) startFlickrFetch 
{ 
// initialize session config and the background session 

[self.FlickrSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) 
{ 
    if(![downloadTasks count]) 
    { 
     NSLog(@"new downloadtask session %@", self.FlickrSession.sessionDescription); 


     NSURLRequest *request = [NSURLRequest requestWithURL:[FlickrFetcher URLforRecentGeoreferencedPhotos]]; 
     // NSURLSessionDownloadTask *task = [self.FlickrSession downloadTaskWithURL:[FlickrFetcher URLforRecentGeoreferencedPhotos]]; 
     NSURLSessionDownloadTask *task = [self.FlickrSession downloadTaskWithRequest:request]; 
     task.taskDescription = FLICKR_DOWNLOAD_TASK; 
     NSLog(@"new request %@", request); 
     NSLog(@"new downloadtask %@", task); 
     [task resume]; 
     //task?[task resume]:[self fireBackgroungFetchCompletionHandler];; 
      //[self fireBackgroungFetchCompletionHandler]; 
     NSLog(@"queue in task %@", dispatch_get_current_queue()); 
    } 
    else 
    { 
     NSLog(@"resuming old downloadtask %d", [downloadTasks count]); 
     for(NSURLSessionDownloadTask *task in dataTasks) [task resume]; 

    } 

}]; 
NSLog(@"queue outside the block %@", dispatch_get_current_queue()); 

} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    // open the file, if there is no managedContext. this is the case wjere the application was launched directly by user 
    // it did not come from the bckground state; 


    //need to enable background fetch 
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; 


//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentChangedState) name:UIDocumentStateChangedNotification object: self.document]; 
    NSLog(@"in application didfinishlaunching"); 

    [self openDatabaseFile]; 
    [self startFlickrFetch]; 



    return YES; 
} 

-(void) application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 

    self.backgroundFetchCompletionHandler = completionHandler; 
    if(self.document.documentState == UIDocumentStateNormal) 
    { 
     //[self openDatabaseFile]; 

     NSLog(@"in performFetchWithCompletionHandler"); 

     [self startFlickrFetch]; 
    } 

} 

- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler 
{ 


    self.completionhandler = completionHandler; 
    NSLog(@"handle event for backgroundURLsession***********");  
} 
+0

我注意到了類似的行爲。特別是,如果我在創建任務之前中斷並跳過它,我會得到零。如果我在創建任務後中斷,則返回任務。這是在模擬器中。 – Felix

+0

我遇到了同樣的問題(在設備上測試過),相同的症狀。當在''application:performFetchWithCompletionHandler:''中運行''downloadTaskWithRequest:''時,你會得到零。 – desudesudesu

+0

我也看到這發生在一個真實的設備上。 (iOS 7.0.3) – TJez

回答

0

這可能與背景獲取和後臺會話的奇怪交互有關,在這種情況下,我沒有任何建議。

但是,在後臺,iOS不知道等待異步調用,例如, getTasksWithCompletionHandler。你可以通過用UIBackgroundTaskIdentifier†基開始/結束任務(在這種情況下,在「獲取(會話)任務完成處理程序」塊中的「結束(應用)任務」)打包這些呼叫來解決此問題。

但是,如果你需要的是一個數,這裏是我做什麼,我認爲這是簡單的:

創建伊娃:

NSMutableSet *activeTaskIDs; 

當您創建一個任務,將其添加到組:

[activeTaskIDs addObject:@(task.taskIdentifier)]; 

當任務完成時,將其刪除。

你可以從那裏得到你的計數,沒有異步。

Conf令人困惑,不同種任務。我用術語「應用任務」與「會話任務」區分開來。