5

3 つの異なるデータ要求を行うために使用しているこのdispatch_queueコードがあります。tableView次に、メインスレッドで更新しています。メインスレッドには他に何を入れることができますか? これを使用し[self requestExtendedData];てWebサービスからデータをダウンロードし、それを解析してUILabelsなどに設定しています...

私の質問は:[self requestExtendedData];バックグラウンド スレッドで実行している場合、メイン スレッドでこのデータのコンテンツを使用して UILabels を更新するにはどうすればよいですか? 他のすべてをメインスレッド領域に配置する必要がありますか? すべての UIView、UILabels、UIButton オブジェクトなど...

助けてくれてありがとう

dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);

// execute a task on that queue asynchronously
dispatch_async(jsonParsingQueue, ^{

    [self requestUserData]; // request user comments data
    [self requestExtendedData]; // request extended listing info data
    [self requestPhotoData]; // request photo data


    // once this is done, if you need to you can call
    // some code on a main thread (delegates, notifications, UI updates...)
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];


    });
});

// release the dispatch queue
dispatch_release(jsonParsingQueue);
4

5 に答える 5

2

これはアップルのドキュメントが言っていることです:

注: ほとんどの場合、UIKit クラスはアプリケーションのメイン スレッドからのみ使用する必要があります。これは特に、UIResponder から派生したクラス、または何らかの方法でアプリケーションのユーザー インターフェイスを操作することを伴うクラスに当てはまります。

つまり、すべてのUIViewUILabelsおよびUIButtonオブジェクト コードを次のようにメイン スレッドに配置する必要があります。

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
    // Your UI update code here
});
于 2013-08-10T01:03:05.317 に答える
0

dispatch_async の後にこのようなことを試してください

[self performSelectorOnMainThread:@selector(doUIStuffOnMainThread:)
                       withObject:self.tableView.data 
                    waitUntilDone:YES];
于 2013-08-09T22:17:31.600 に答える
0
[your_tableview performSelectorOnMainThread:@selector(reloadData)
                                     withObject:nil
                                  waitUntilDone:YES];
于 2013-08-10T07:02:08.363 に答える