2014-01-07 25 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令人困惑,不同种任务。我用术语“应用任务”与“会话任务”区分开来。