NSScreencast Episode #93 Background Transfers
Background Transfers - NSScreencast
画像のダウンロードをバックグラウンドで実行
- downloadTapped
- if (!self.session) {
- self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
- NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:imageUrl];
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- [task resume];
- if (!self.session) {
- NSURLSessionDownloadDelegate
- didFinishDownloadingToURL
- didWriteData
- didResumeAtOffset
- URLSession didCompleteWithError
- notification
- AppDelegate handleEventsForBackgroundURLSession
- NSDictionary *userInfo = @{@"sessionIdentifier": identifier, @"completionHandler": completionHandler};
- [[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundSessionUpdated"
- AppDelegate handleEventsForBackgroundURLSession
- vc viewWillAppear
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(handleBackgroundSession:)
- handleBackgroundSession notification
- if ([notification.userInfo[@"sessionIdentifier"] isEqualToString:self.session.configuration.identifier]) {
- self.backgroundCompletionHandler = notification.userInfo[@"completionHandler"];
- if ([notification.userInfo[@"sessionIdentifier"] isEqualToString:self.session.configuration.identifier]) {
- [[NSNotificationCenter defaultCenter] addObserver:self
- URLSessionDidFinishEventsForBackgroundURLSession
- self.backgroundCompletionHandler();
- self.backgroundCompletionHandler = nil;