使NSOperationQueue同步

时间:2015-06-29 17:38:38

标签: ios objective-c sync nsoperation nsoperationqueue

如何让NSOperationQueue同步?我通过继承NSOperation并设置" setMaxConcurrentOperationCount"使用" addDependency"还可以在以前的操作中添加依赖关系。方法

添加代码:

if(!operationQueue)
{
    operationQueue = [[MyQueue alloc] init];
}
[operationQueue setMaxConcurrentOperationCount:1];
UploadFileOperation *uploadFileOperation = [[UploadFileOperation alloc] initWithObject:someObject];
[operationQueue addOperation:uploadFileOperation];

现在,当记录两个文件的上传进度时,它会显示两个文件同时上传。像这样: File1 - 17.647026%已上传 File1 - 18.352907%已上传 File2 - 0.870381%已上传 File2 - 1.740762%已上传 File2 - 2.611142%已上传 File2 - 3.481523%已上传 File1 - 19.058788%上传

编辑2: 这就是我实现NSOperation主要方法的方法:

- (void)main {
@autoreleasepool {
    if(self.isCancelled)
    {
        return;
    }

    [[[NetworkManager alloc] init] uploadFileToAmazon:someURL WithRequestType:networkRequestType WithCompletion:^(NetworkRequestType _networkRequestType, id response, NSError *error) {
        NSLog(@"response: %@", response);
        NSLog(@"Error: %@", error);
       if(uploadFileDelegate && [uploadFileDelegate respondsToSelector:@selector(fileUploaded:)])
       {
           [uploadClipDelegate fileUploaded:response];
       }
    }];
}
}

我还使用" AFAmazonS3Manager"用于上传文件" uploadFileToAmazon"。

0 个答案:

没有答案