2015-11-04 58 views
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?

回答

1

ephemeralSessionConfiguration:除了所有會話相關數據都存儲在內存中之外,與默認配置類似。把它想象成一個「私人」會話。 backgroundSessionConfiguration:讓會話在後臺執行上傳或下載任務。即使應用程序本身暫停或終止,轉移也會繼續。 更多信息:https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started

相關問題