为什么我们在这里使用ephemeralSessionConfiguration


- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if (self.photoDatabaseContext) {
        NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
        sessionConfig.allowsCellularAccess = NO;
        sessionConfig.timeoutIntervalForRequest = BACKGROUND_FLICKR_FETCH_TIMEOUT;
        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[FlickrFetcher URLforRecentGeoreferencedPhotos]];
        NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request
                                                        completionHandler:^(NSURL *localFile, NSURLResponse *response, NSError *error) {
                                                            if (error) {
                                                                NSLog(@"Flickr background fetch failed: %@", error.localizedDescription);
                                                                completionHandler(UIBackgroundFetchResultNoData);
                                                            } else {
                                                                [self loadFlickrPhotosFromLocalURL:localFile
                                                                                       intoContext:self.photoDatabaseContext
                                                                               andThenExecuteBlock:^{
                                                                                   completionHandler(UIBackgroundFetchResultNewData);
                                                                               }
                                                                 ];
                                                            }
                                                        }];
        [task resume];
    } else {
        completionHandler(UIBackgroundFetchResultNoData);
    }
}

必须有backgroundSessionConfigurationWithIdentifier而不是ephemeralSessionConfiguration似乎是合乎逻辑的,因为它是在后台加载的。但来自斯坦福iOS课程的保罗·赫加蒂(Paul Hegarty(表示,第二更好。为什么?他说了一些关于离散获取的事情,但我不明白。

ephemeralSessionConfiguration:与默认配置类似,不同之处在于所有与会话相关的数据存储在内存中。将此视为"私人"会话。后台会话配置:允许会话在后台执行上载或下载任务。即使应用本身被暂停或终止,转移也会继续。更多信息 :https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started

相关内容

  • 没有找到相关文章

最新更新