。下載零件和文件存儲每次都能正常工作。但更新單元格進度或圖像僅適用於控制器的初始加載。如果我導航到根目錄,然後再次到我的集合控制器下載工作,但細胞不再更新。我發現這個有趣的discussion,但它並沒有幫助我的情況。NSOperationQueue mainQueue不更新UICollectionViewCell
更多細節,啓動URL會話和處理任務的控制器從storyboard segue加載。我注意到應用程序每次都在創建新的控制器實例。在第二次導航訪問單元不顯示進度或更新,但任務/文件正確下載。似乎我的下載任務正在從其他控制器實例獲取單元的副本。但是從調試器中看到的內容沒有意義,代表正在正常工作,任務隨着成功完成。
這裏是代碼的方法未能更新2日訪問細胞控制器:
-(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;
}
什麼能導致細胞的第二個副本或簡單地收集單元不響應第二次導航回合的呼叫或數據重新加載?希望有人對這個問題有一個解決方案。
我曾嘗試這一點,但結果是相同的 – takumi3t9
dispatch_async(dispatch_get_main_queue(),^ { fdi.downloadProgress =(雙)totalBytesWritten /(雙)totalBytesExpectedToWrite; cell.downloadProgress.hidden = NO; 細胞。下載進度.progress = fdi.downloadProgress; NSLog(@「##更新單元進度,ID:%@ content:%@」,cell.testLbl.text,fdi.contentId); }); – takumi3t9
在我之前的項目中,我遇到了同樣的問題,有時候會更新,有些時候不會,所以我通過使用UIView和更新幀寬自定義了一個。 – Nick