-3

リモートサーバー上の複数のソースからの複数のJSON文字列を再度解析する必要があり、単一の解析の各値をUITableViewに入力する必要があります。@Heのおかげで前の回答が得られました。

単一のJSONソース、例:h p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A1:**

{
  "id": 0001,
  "main": {
    "mainA": 100,
  },
}

単一のJSONソース、例:h p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A2:**

{
  "id": 0002,
  "main": {
    "mainA": 200,
  },
}

単一のJSONソース、例:h p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A3:**

{
  "id": 0003,
  "main": {
    "mainA": 300,
  },
}

目標: mainAの各値を解析し、それらを正しい順序で取得するA1-> A2-> A3

リモートJSON文字列を解析できる単一のURLの配列:

idURL = [[NSArray alloc] initwithObjects:@"A1", @A2", @A3", nil];

解析、@ Sulthanのおかげで、ブロックが必要です'cosENDがtheGreatMethodを呼び出すのを待たなければなりません:

- (void)parsingJSON { // starts with a UIButton pressed!

    mainA = [@[@"1",@"2",@"3"] mutableCopy]; // defined here to preserve right order of mainA objects after parsing

    int mainACount = 0;

    typedef void (^AFJSONSuccessBlock) (NSURLRequest *request, NSHTTPURLResponse *response, id JSON);
    typedef void (^AFJSONFailureBlock) (NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON);

    __block int numRequests = [idURL count];

    AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSMutableDictionary *dictMain = [JSON objectForKey:@"main"];
        [mainA replaceObjectAtIndex:mainACount withObject:[dictMain objectForKey:@"mainA"]];

        mainACount ++; // EVERY AFJSONRequestOperation *operation int mainACount = mainACount + 1 
        numRequests --; // EVERY AFJSONRequestOperation *operation int numRequest = numRequest - 1

        if (numRequests == 0) {

            NSLog(@"END PARSING");
            NSLog(@"FINAL mainA ORDER: %@",mainA);

        [self theGreatMethod]; // a method to populate UITableView with mainA results

        }
    };

    AFJSONFailureBlock onFailure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        NSLog(@"ERROR: %@", [error userInfo]);

        numRequests--;

        if (numRequests == 0) {

         NSLog(@"ERROR PARSING");
            [self theGreatMethod]; // a method to populate UITableView with mainA results

        }
    };

    for (NSString* jsonPath in idURL) {

        NSString* absolutePath = [NSString stringWithFormat:@"h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@", jsonPath];
        NSURL *url = [NSURL URLWithString:absolutePath];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFJSONRequestOperation *operation;
        operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                    success:onSuccess
                                                                    failure:onFailure];
        [operation start];

    }

        NSLog(@"START PARSING");

}

この状況で、各AFJSONRequestOperation *操作サイクルをカウントし、mainAオブジェクトを1つずつ置き換えるためにint mainACountを定義しましたが、これが正しい方法かどうかはわかりません。(int i = 0; i <[mainA count]; i ++)および[mainA replaceObjectAtIndex:i]ループも試しましたが、同じ間違った結果になりました。

NSLog(@ "FINAL mainA ORDER:%@"、mainA);の結果

1st parsing -> NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
2nd parsing -> (after a few seconds) NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
3rd parsing -> (after other few seconds) NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
...

エラー:UIButtonを押して解析を開始するたびに確認できるように、mainAオブジェクトの異なるシーケンス(テーブル行の異なる値)が再び表示されます。例:最初の解析後、何を期待しますか?100-200-300、代わりにランダム...では、もう一度助けてもらえますか、ここで何が問題になっていますか?または、正しい順序でその解析を行うためのより良い方法がありますか?

4

1 に答える 1

1

次のように URL の配列を宣言してみてください。

idURL = [[NSArray alloc] initwithObjects:@"A1", @"A2", @"A3", nil];

を使用しているため、forループがオブジェクトを順番に解析したとしても、非同期リクエストであるため、 へのNSArray呼び出しがAFJSONRequestOperation結果を返す順序は保証されません。

@synchronized ディレクティブを使用してスレッドをロックすることができます。

- (void)parsingJSON:(id)anObj
{
    // ...
    AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // ...
        @synchronized(anObj)
        {
            // Everything between the braces is protected by the @synchronized directive.
            var2 ++; // EVERY AFJSONRequestOperation *operation int var2 = var2 + 1 
            numRequests --; // EVERY AFJSONRequestOperation *operation int numRequest = numRequest - 1
        }
        // ...
    }
    // ...
}
于 2013-02-07T13:06:20.850 に答える