簡體   English   中英

Objective-C GCD等待塊完成(AFNetworking)

[英]Objective-C GCD wait for Block to finish (AFNetworking)

我一直在嘗試使用教程中的一些代碼,但是由於不了解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執行嗎?

不確定要完成什么,但是如果目標是僅在完成塊之后執行NSLog,則可以將NSLog語句移到

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

如果您有一些要添加到數組后要執行的代碼,則可以

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

完成塊是在您要運行的代碼執行后運行的代碼。 您要運行的代碼將異步發生在另一個線程上。 運行該代碼后,將執行完成塊代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM