我知道这个问题在SO上被多次询问过,但我没有设法让它在我的项目中发挥作用......
所以,我想使用子类NSOperation并使其下载文件NSURLConnection.做正确的方法是什么?这是我的代码不起作用:首先,我在循环中添加所有操作:
DownloadFileOperation *operation;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
for (int i=0; i<10; i++) {
operation = [[DownloadFileOperation alloc] init];
operation.urlString = pdfUrlString;
[queue addOperation:operation];
operation = nil; }
Run Code Online (Sandbox Code Playgroud)
这是我的子类:
@interface DownloadHandbookOperation : NSOperation <NSURLConnectionDelegate>
{
}
@property (strong, nonatomic) NSString *urlString;
@end
@implementation DownloadHandbookOperation
{
NSString *filePath;
NSFileHandle *file;
NSURLConnection * connection;
}
- (void)start
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
NSURL *url = [[NSURL alloc] initWithString:[self.urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; …Run Code Online (Sandbox Code Playgroud)