Reputation: 91
- (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);
}
}
It seems logical that there must be backgroundSessionConfigurationWithIdentifier
instead of ephemeralSessionConfiguration
because it is loading in the background. But Paul Hegarty from Stanford iOS course said that second is better. Why? He said something about discrete fetching, but I didn't understand.
Upvotes: 0
Views: 1613
Reputation: 1212
ephemeralSessionConfiguration: Similar to the default configuration, except that all session-related data is stored in memory. Think of this as a “private” session. backgroundSessionConfiguration: Lets the session perform upload or download tasks in the background. Transfers continue even when the app itself is suspended or terminated. more info :https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started
Upvotes: 1