【问题标题】:Creating dependencies between operations when using AFHTTPRequestOperation使用 AFHTTPRequestOperation 时创建操作之间的依赖关系
【发布时间】:2014-09-10 07:36:18
【问题描述】:

我在 mac 应用程序中使用 AFNetworking (2.4.1)。我希望在完成所有其他操作(AFHTTPRequestOperation)之后添加我自己的块操作。我已经尝试在 completionOperation 和其他之间添加依赖关系,但是在其他成功或失败完成之前,completionOperation 块仍然执行。

说明基本知识的代码的精简版本如下。有没有人能建议如何使这项工作?谢谢。

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

    NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"All operations complete");
        }];


    NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[[NSURL URLWithString:@"https://api.parse.com/1/classes/SomeClass"] absoluteString] parameters:nil error:nil];
    AFHTTPRequestOperation *operation1 = [manager HTTPRequestOperationWithRequest:request1 success:
    ^(AFHTTPRequestOperation *operation, id responseObject)
    {
            NSLog(@"operation 1 success");
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
            NSLog(@"operation 1 failure");
    }];

    NSMutableURLRequest *request2 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[[NSURL URLWithString:@"https://api.parse.com/1/classes/OtherClass"] absoluteString] parameters:nil error:nil];
    AFHTTPRequestOperation *operation2 = [manager HTTPRequestOperationWithRequest:request2 success:
    ^(AFHTTPRequestOperation *operation, id responseObject)
    {
            NSLog(@"operation 2 success");
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
            NSLog(@"operation 2 failure");
    }];

    [completionOperation addDependency:operation1];
    [completionOperation addDependency:operation2];

    [manager.operationQueue addOperation:operation1];
    [manager.operationQueue addOperation:operation2];
    [manager.operationQueue addOperation:completionOperation];

【问题讨论】:

  • 请修正缩进。
  • 我敦促您不要使用不再维护的 AFHTTPRequest。
  • 嗨@SeanChense。您是指我正在使用的特定类还是框架?我在这里使用的类是文档中 AFNetworking 批处理示例中使用的类。这很像提供的示例,除了我没有使用 batchOfRequestOperations: 因为我想尝试设置我自己的操作之间的依赖关系。
  • @TimZimmermann - 额外的缩进被移除。

标签: objective-c macos afnetworking objective-c-blocks


【解决方案1】:
    - (void) test3:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

  // block 1
    NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *urll = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:urll];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"OPERATION 1  %@",responseObject );
        test_Sync = @"done";
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        test_Sync = @"faile"; }];

    //block 2
    NSString *string2 = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *urll2 = [NSURL URLWithString:string2];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:urll2];

    AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];

    [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation2, id responseObject) {
        // Print the response body in text
        NSLog(@"Response: OPERATION 2 %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    } failure:^(AFHTTPRequestOperation *operation2, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    // Add the operation to a queue
    // It will start once added
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    // Make operation2 depend on operation1
    [operation addDependency:operation2];
    [operationQueue addOperations:@[operation, operation2] waitUntilFinished:YES];

}

【讨论】:

  • 您能解释一下如何回答这个问题吗?
  • 是的,当然!所以在我的例子中,我使用了 2 个块,第一个块在第二个块完成时开始,这与 DEPENDECY : "希望对你有帮助
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 2015-04-22
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多