0

AFNetworking を使用して、ブロック内で同期的に JSON を受信できません。この解決策を確認しました。メソッドの最後では常に nil です。

これが私の方法です:

- (BOOL)whois:(NSString *)domain withZone: (NSString*) zone
{        
    __block NSString *resultCode;

    NSURL *url = [[NSURL alloc] initWithString:@"myurl"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {     
        resultCode = [JSON valueForKeyPath:[NSString stringWithFormat:@"%@.%@", domain,zone]];  //checked with NSLog, works well              
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation: operation];
    [operation waitUntilFinished];

    if(resultCode == @"available") //nil here
    {
        return YES;
    }
    return NO; 
}
4

2 に答える 2

0

を作成する代わりに、 withをNSOperationQueue開始してから呼び出すと、終了するまでメインスレッドがブロックされます。その場合、resultCode は nil であってはなりません。AFJSONRequestOperation[operation start][operation waitUntilFinished]

リンクした投稿で @matttが言ったように、このようにスレッドを凍結することは強くお勧めしません。成功ブロックから続行したい新しいメソッドを呼び出したり、失敗ブロックから別の失敗メソッドを呼び出したりするなど、これを行う別の方法を考え出すことを検討してください。

于 2012-11-13T01:27:34.813 に答える
0

メソッドは現在の設計では機能しません。

- (BOOL)whois:(NSString *)domain withZone: (NSString*) zone
{        
    __block NSString *resultCode;

    NSURL *url = [[NSURL alloc] initWithString:@"myurl"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    // *** Runs 1st
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {     

        // *** runs 3rd
        resultCode = [JSON valueForKeyPath:[NSString stringWithFormat:@"%@.%@", domain,zone]];  //checked with NSLog, works well              
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    // *** Runs 2nd
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation: operation];
    [operation waitUntilFinished];

    if(resultCode == @"available") //nil here
    {
        return YES;
    }
    return NO; 
}

ブロック内のマテリアルは 3 番目に非同期で実行されるため、現在設計されている方法でその値をより大きなメソッドに返すことはできません。おそらく次のようなものを使用します。

- (void)whois:(NSString *)domain withZone: (NSString*) zone
{        

    NSURL *url = [[NSURL alloc] initWithString:@"myurl"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    __weak id weakSelf = self;
    // Runs 1st
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {     

        NSString *resultCode = [JSON valueForKeyPath:[NSString stringWithFormat:@"%@.%@", domain,zone]];  //checked with NSLog, works well       
        [weakSelf receivedResultCode:resultCode];       
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];
}

- (void) receivedResultCode:(NSString *)resultCode {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation: operation];
    [operation waitUntilFinished];

    if(resultCode == @"available") //nil here
    {
        // do @YES stuff
    }
    else {
        // do @NO stuff
    }
}

指定した方法で値を返さないため、呼び出し元の設計を変更する必要があることは明らかです。もっと良い解決策があるかもしれませんが、これが機能するために必要なタイプの設計だと思います。

于 2014-04-07T20:12:00.937 に答える