如何触发 NSOperationQueue 并在多个操作块中获取结果 [英] How to trigger NSOperationQueue and get results in block of multiple operation

查看:63
本文介绍了如何触发 NSOperationQueue 并在多个操作块中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过与 NSOperationQueue 相关的问题,但我仍然在用多个操作实现操作队列.我有以下代码

I already asked question related to NSOperationQueue but I am still around of implementing operation queue with multiples operation. I have following code

    NSMutableArray * operationArray = [[NSMutableArray alloc] init];
for (int i =0; i<[documentModelList count]; i++) {
    DocumentModel * documentModel = [documentModelList objectAtIndex:i];
    NSString *url = [NSString stringWithFormat:@"%@%@/%li", SERVER_URL, DOCUMENTS_DELETE,(long)documentModel.documentID];
    [operationArray addObject:[AppHttpClient getDeleteRequest:nil urlQuery:url]];
}
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:operationArray.count];
[operationQueue addOperations:operationArray waitUntilFinished:NO];



+ (AFHTTPRequestOperation *) getDeleteRequest:(NSDictionary *)headerParams urlQuery: (NSString*)action
{

NSString *jsonString = @"";
NSString *authorizationValue = [self setAuthorizationValue:action];
NSString *language = @"en_US";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:language forHTTPHeaderField:@"Accept-Language"];
[request setValue:authorizationValue forHTTPHeaderField:@"authorization"];

[request setURL:[NSURL URLWithString:action]];
[request setTimeoutInterval:500.0];
[request setHTTPMethod:@"DELETE"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

return operation;
}

以上代码在循环中创建操作并添加到operationArray,然后将此操作数组添加到operationQueue.现在我想触发它​​并获得整个阵列的响应.

The above code creating operations in loop and adding into operationArray and then add this operation array into operationQueue. Now I want to trigger that and get response of whole array.

已编辑

+ (void) gernalDeleteRequest:(NSDictionary *)headerParams urlQuery: (NSString*)action parameters:(NSDictionary*)params
            onComplete:(void (^)(id json, id code))successBlock
               onError:(void (^)(id error, id code))errorBlock
{
    NSString *jsonString = @"";
    NSString *authorizationValue = [self setAuthorizationValue:action];
    NSString *language = @"en_US";



NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:language forHTTPHeaderField:@"Accept-Language"];
[request setValue:authorizationValue forHTTPHeaderField:@"authorization"];

//convert parameters in to json data

if ([params isKindOfClass:[NSDictionary class]]) {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

[request setURL:[NSURL URLWithString:action]];
[request setTimeoutInterval:500.0];
[request setHTTPMethod:@"DELETE"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSInteger statusCode = [operation.response statusCode];
    NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];
    successBlock(responseObject, statusObject);
    NSLog(@"authentication success");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSInteger statusCode = [operation.response statusCode];
    NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];

    id responseObject = operation.responseData;
    id json = nil;
    NSString *errorMessage = nil;

    if (responseObject) {

        json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
        errorMessage = [(NSDictionary*)json objectForKey:@"Message"];
    }else{
        json = [error.userInfo objectForKey:NSLocalizedDescriptionKey];
        errorMessage = json;
    }

    errorBlock(errorMessage, statusObject);

}];

[[NSOperationQueue mainQueue] addOperation:operation];

}

推荐答案

对于整体状态可以创建另一个操作,可以是块操作,并且使用 addDependency: 来确保它在所有其他操作完成后运行.在创建每个删除操作的循环中添加依赖项.

For the overall status you can create another operation, which can be a block operation, and which uses addDependency: to ensure that it runs after all of the other operations are complete. Add the dependencies in the loop where you create each delete operation.

对于每个单独的状态,您需要使用 setCompletionBlockWithSuccess:failure: 来获取有关结果的反馈.

For each individual status you need to use setCompletionBlockWithSuccess:failure: to get feedback about the results.

这篇关于如何触发 NSOperationQueue 并在多个操作块中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆