2017-09-02 8 views
0

データを取り込もうとしているtableViewがあります。関数内の配列に値を代入しています。 searchWithLocationの機能が終了するまで、すべてが保存されます。ログを見ると、データの順序が十分に速くならないように、注文と関係していると思います。それとも、それは理由ではない、私は本当に分かりません。NSMutable配列は、終了後にデータを保持しません。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    venueName = [[NSMutableArray alloc] init]; 

    [YLPClient authorizeWithAppId:@"sdgsfgfgfg" 
          secret:@"aegsgdgdfs" 
       completionHandler:^ 
    (YLPClient *client, NSError *error) { 
     // Save your newly authorized client 
     self.client = client; 

     [self.client searchWithLocation:@"Chicago" 
         completionHandler:^ 
      (YLPSearch *search, NSError *error) { 
       for (YLPBusiness *business in search.businesses) { 
        [venueName addObject:business.name]; 
       } 
       NSLog(@"I return results: %lu", (unsigned long)[venueName count]); 
      }]; 
     NSLog(@"I return 0: %lu", (unsigned long)[venueName count]); 
    }]; 
} 

... 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"number of rows in section: %lu", (unsigned long)[venueName count]); 
    return [venueName count]; 
} 

ログ:

2017-09-02 14:46:39.708 whatever[67890:8535793] number of rows in section: 0 
2017-09-02 14:46:39.714 whatever[67890:8535793] number of rows in section: 0 
2017-09-02 14:46:39.715 whatever[67890:8535793] number of rows in section: 0 
2017-09-02 14:46:40.448 whatever[67890:8535846] I return 0: 0 
2017-09-02 14:46:41.174 whatever[67890:8535846] I return results: 20 

答えて

1

あなたは、非同期プログラムの実行の誤解を持っています。完了ハンドラは、呼び出し元の実行パスが既に終了した後で実行できます。あなたの場合:

[self.client searchWithLocation:@"Chicago" 
       completionHandler: 
    // This completion handler can be executed after seconds, minutes or even hours. 
    ^(YLPSearch *search, NSError *error) 
    { 
    NSLog(@"As times go by – sometime in the future"); 
    for (YLPBusiness *business in search.businesses) 
    { 
     [venueName addObject:business.name]; 
    } 
    NSLog(@"I return results: %lu", (unsigned long)[venueName count]); 
    }]; 
    // This path is executed immediately. esp. likely before the cmpletion handler is executed 
    NSLog(@"It is now or never"); 
    NSLog(@"I return 0: %lu", (unsigned long)[venueName count]); 

コード内の完了ハンドラの後ろにコードを移動します。

+0

私はtableViewCellを更新したい場合は、completionHandlerが起動し、その中でいくつかの更新メソッドを呼び出すまで待つ必要がありますか?申し訳ありませんが、私がしようとしていることが意味をなさない場合、私はウェブの背景から来ています。 – itsclarke

+0

Ahh okay cool私はそれを理解した、私はちょうどあなたが言ったことを見直してそれを話さなければならなかった。どうもありがとう。 – itsclarke

関連する問題