gpt4 book ai didi

ios - 从 NSOperation 的子类中调用 NSURLSession 方法

转载 作者:行者123 更新时间:2023-11-29 12:02:38 24 4
gpt4 key购买 nike

我的应用程序我有带有自定义单元格的 TableView ,它有:

@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *nameOfImage;
@property (weak, nonatomic) IBOutlet UIButton *start;

我需要异步下载任意数量的图像(通过按很多开始按钮)。为此,我创建了 NSOperation 的子类 - MyOperationQueue。它的实现看起来像这样:

- (id)initWithURL:(NSURL*)url andRaw:(NSInteger)row
{
if (![super init])
return nil;
[self setTargetURL:url];
[self setCurrentCell:row];
self.customCell = [[CustomTableViewCell alloc]init];
self.tableView = [ContentTableView sharedManager];
return self;
}

- (void)main
{
if ([self isCancelled])return;
self.defaultSession = [self configureSession];
NSURLSessionDownloadTask *task = [self.defaultSession downloadTaskWithURL:self.targetURL];
if ([self isCancelled]) return;
[task resume];
}

- (void)cancel
{
self.isCancelled = YES;
if(self.downloadTask.state == NSURLSessionTaskStateRunning)
[self.downloadTask cancel];
}

在这些方法中,我还在这里描述了 NSURLSession 方法:

    - (NSURLSession *) configureSession //it visits it
{
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self.tableView **delegateQueue:self**]; //self - warning - incompatible pointer type sending "MyOperationQueue" to "NSOperationQueue" _Nullable.

}

-(void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite // don't visit

-(void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location // don't visit

我在 TableView 的方法中创建自定义队列:

   - (void)didClickStartAtIndex:(NSInteger)cellIndex withData:(CustomTableViewCell*)data
{
NSURL *url = [NSURL URLWithString: tmp.imeageURL];
MyOperationQueue * one = [[MyOperationQueue alloc]initWithURL:url andRaw:self.selectedCell];
[self.queue addOperation:one];
}

图像根本没有加载,队列只是进入配置方法而已。请告诉我怎么了?提前谢谢你。

编辑:更新用户界面:

-(void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"didFinishDownloadingToURL - Queue");
NSData *d = [NSData dataWithContentsOfURL:location];
UIImage *im = [UIImage imageWithData:d];
dispatch_async(dispatch_get_main_queue(), ^{
self.customCell.image.image = im;
self.customCell.realProgressStatus.text = @"Downloaded";
[self.tableView.tableView reloadData];
});
}

最佳答案

你的类是 NSOperation 的子类,而不是 NSOperationQueue 所以你不能指定它使用 self 作为目标队列NSURLSession 对象。坦率地说,您只需将 nil 用作操作队列参数,因为您不关心委托(delegate)方法在哪个队列上运行。或者,如果需要,您可以创建自己的操作队列。但是你不能使用 self

但请确保将任何 UI 更新分派(dispatch)回主队列。

此外,您必须将其设为异步 NSOperation 子类,否则该操作将终止并在您的委托(delegate)方法有机会被调用之前被释放。

作为一个小观察,我建议将此类重命名为 MyOperation,以避免操作和操作队列之间的混淆。

关于ios - 从 NSOperation 的子类中调用 NSURLSession 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36321356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com