【问题标题】:Objective-C GCD wait for Block to finish (AFNetworking)Objective-C GCD 等待 Block 完成(AFNetworking)
【发布时间】:2012-11-06 15:13:34
【问题描述】:

我一直在尝试使用教程中的一些代码,但由于没有深入了解 GCD,因此没有取得多大成功。

我有一个名为 API.m 的类,这里是关于 GCD 的代码:

+ (API *) sharedInstance
{
    static API *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:APIHost]];
    });

    return sharedInstance;
}

-(void)commandWithParams:(NSMutableDictionary*)params
            onCompletion:(JSONResponseBlock)completionBlock
{
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST"
                                                                      path:APIPath
                                                                parameters:params
                                                 constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                                     //TODO: attach file if needed
                                                 }];

    AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success!
        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //failure :(
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];

    [operation start];
}

我通过实现一个按钮并让一个 NSArray 将其内容打印到输出窗口来进行简单的测试:

- (IBAction)test:(id)sender {

    NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                  @"pending", @"command",
                                  [[[API sharedInstance] user] objectForKey:@"UserID"] , @"userID",
                                  nil];

    [[API sharedInstance] commandWithParams:params
                               onCompletion:^(NSDictionary *json) {
                                   //result returned

                                   if ([json objectForKey:@"error"]==nil) {
                                       // Simple example
                                       [self.users addObject:@"1"];

                                   } else {
                                       //error
                                       [UIAlertView title:@"Error" withMessage:[json objectForKey:@"error"]];
                                   }

                               }];
    NSLog(@"%@", self.users);
}

现在,当我第一次单击按钮时,一个空的 NSArray 会打印到输出窗口,但是当我再次按下它时,它会打印“1”。很明显,程序在完成块有时间完全执行之前到达 NSLog。有人可以帮我修改代码,以便我可以选择在完成块完成后执行 NSLog 吗?

【问题讨论】:

  • 块的这种用法的全部内容是立即返回。如果你不想要它,使用不同的东西。

标签: iphone objective-c xcode grand-central-dispatch afnetworking


【解决方案1】:

不确定您要完成什么,但如果目标是在完成块之后执行 NSLog,您可以将 NSLog 语句移到之后

[self.users addObject:@"1"];

如果你有一些代码要在添加到数组后执行,你可以有

[self methodName]; in the completion block and it will get called there.

完成块,是在执行您想要运行的代码之后运行的代码。您想要运行的代码将在另一个线程上异步发生。运行该代码后,将执行完成块代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    相关资源
    最近更新 更多