0
- (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?