在我目前的设计中,我有一个表示文件夹结构的tableview。当用户点击单元格时,将创建一个AFHTTPRequestOperation
以下载文件。当单元格当前显示当前下载状态时,下载该文件:无/下载/下载。AFNetworking完成块
下载状态设置在对应于每个单元的NSManagedObject
上。在我的下载完成块中,我将下载状态设置为“已下载”标志。问题在于如果用户从当前的tableview数据导航到另一个,竞争块将设置错误的NSManagedObject
- 它根据NSIndex
进行查找。
简而言之,我想知道如何将对象与我的AFHTTPRequestOperation
一起传递,以便在完成时可以对其执行操作。它可以像int或string一样简单,我可以根据此值执行NSPredicate
请求。
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[self.operations addObject:operation];
[self.inProgressDownloads addObject:indexPath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *extension = [[self.selectedFile.name componentsSeparatedByString:@"."] lastObject];
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@", self.selectedFile.item_id, extension]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
self.selectedFile.status = DOWNLOADING;
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"File downloaded to: %@", path);
self.selectedFile.status = DOWNLOADED;
self.selectedFile.is_stored = @YES;
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
self.selectedFile.status = DOWNLOAD_ERROR;
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
dispatch_async(dispatch_get_main_queue(), ^{
float percent = (float)totalBytesRead/(float)totalBytesExpectedToRead;
NSLog(@"totalBytesExpectedToRead %d", (int)(percent * 100));
});
}];
[operation start];
请检查我编辑的答案,我已经为您的特定情况添加了解决方案。 – sergio
棒极了,塞尔吉奥。这是我的问题,错误的范围!谢谢! – Shocks
很高兴有帮助。 – sergio