【发布时间】:2015-12-30 04:04:24
【问题描述】:
在使用NSURLSession 下载文件并在UICollectionView 中显示它们时,我发现了一个问题,即单元格无法从NSOperationQueue 块正确更新。下载部分和文件存储每次都能正常工作。但是更新单元格进度或图像仅适用于控制器的初始加载。如果我导航到 root,然后再次导航到我的 Collection 控制器,下载工作但单元格不再更新。我发现这很有趣discussion,但对我来说没有帮助。
更多细节,启动 URL 会话和处理任务的控制器从故事板 segue 加载。我注意到应用程序每次都在创建新的控制器实例。在第二次导航访问单元格不显示进度或更新,但任务/文件已正确下载。似乎我的下载任务正在从其他控制器实例获取单元格的副本。但从我在调试器中看到的情况来看,这没有意义,委托工作正常,任务成功完成。
这是在第二次访问控制器时无法更新单元格的代码方法:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
NSLog(@"Unknown transfer size");
}
else{
// Locate the FileDownloadInfo object in array based on my unique key in URL.
int index = [self getFileDownloadInfoIndexWithTaskURL:downloadTask.originalRequest.URL.lastPathComponent];
// Get singleton download object list from app delegate
NSMutableArray *globalFileDownloadData = [self getGlobalDownloadFileList];
FileDownloadInfo *fdi = [globalFileDownloadData objectAtIndex:index];
// Get the progress view of the appropriate cell and update its progress.
NSArray *arr = [self.fileDispCollection visibleCells];
for(FileItemCell* cell in arr){
if ([fdi.contentId isEqualToString:cell.testLbl.text]){
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// This code works when app start initial visit to controller
// but does not work on2nd 3rd or other revisit
// even cells found correctly, files also loaded saved
// the progress does not show and no update
cell.downloadProgress.hidden = NO;
cell.downloadProgress.progress = fdi.downloadProgress;
}];
}
}
}
}
我使用这种方法初始化我的单例会话:
- (NSURLSession *)backgroundSession {
//disptach_once ensure that multiple background sessions are not
//created in this instance of the application.
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
NSArray *URLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
self.docDirectoryURL = [URLs objectAtIndex:0];
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.Transport.demo"];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 3;
sessionConfiguration.discretionary = NO;
session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
});
return session;
}
什么会导致第二个单元格副本或只是收集单元格在第二轮导航中不响应调用或数据重新加载?希望有人能解决这个问题。
【问题讨论】:
标签: objective-c navigation uicollectionview nsurlsessiondownloadtask