为什么我们在这里使用 ephemeralSessionConfiguration?
Why we are using ephemeralSessionConfiguration here?
- (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:与默认配置类似,只是所有session-related数据都存储在内存中。将其视为“私有”session。
backgroundSessionConfiguration:让session在后台执行上传或下载任务。即使应用程序本身被暂停或终止,传输也会继续。
更多信息:https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started
- (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:与默认配置类似,只是所有session-related数据都存储在内存中。将其视为“私有”session。 backgroundSessionConfiguration:让session在后台执行上传或下载任务。即使应用程序本身被暂停或终止,传输也会继续。 更多信息:https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started