2

Web サイトから XML データを取得するために TBXML+HTTP を使用しています。私がやりたいことは、処理されたデータを UITableView に取り込むことです。すべてのエントリを保持する NSMutableArray を作成しましたが、それに関する限り、すべて正常に動作しています。問題は、サーバーからデータを正常にフェッチして配列に格納した後、 を使用してテーブルを更新してデータを表示しようとしたときreloadDataです。

テーブルに 20 行を入力するのに約 5 秒かかります。奇妙な点は、フェッチが非常に高速に行われるため、データがすぐに利用できることです。何がそんなに時間がかかるのか理解できません。ログからの情報は次のとおりです。

2012-03-17 18:46:01.045 MakeMyApp[4571:207] numberOfSectionsInTableView: 1
2012-03-17 18:46:01.047 MakeMyApp[4571:207] numberOfRowsInSection: 0
2012-03-17 18:46:01.244 MakeMyApp[4571:1f03] numberOfSectionsInTableView: 1
2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] numberOfRowsInSection: 20
2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] Ok, I'm done. 20 objects in the array.
2012-03-17 18:46:01.246 MakeMyApp[4571:1f03] Finished XML processing.
2012-03-17 18:46:06.197 MakeMyApp[4571:1f03] cellForRowAtIndexPath:

ご覧のとおり、ペアを2 回起動しnumberOfSectionsInTableView:ますnumberOfRowsInSection:。1 回目はビューの読み込み時、2 回目はビューの読み込み時です。[self.tableView reloadData];

1 秒もかからずに、配列に 20 個のオブジェクトが取り込まれ、XML を処理するすべての作業が完了することがわかります。cellForRowAtIndexPath:わずか5秒後に発射される方法は?

問題を見つけるのに役立つ可能性のあるコードの一部を次に示します。

- (void)createRequestFromXMLElement:(TBXMLElement *)element {
    // Let's extract all the information of the request
    int requestId = [[TBXML valueOfAttributeNamed:@"i" forElement:element] intValue];

    TBXMLElement *descriptionElement = [TBXML childElementNamed:@"d" parentElement:element];
    NSString *description = [TBXML textForElement:descriptionElement];

    TBXMLElement *statusElement = [TBXML childElementNamed:@"s" parentElement:element];
    int status = [[TBXML textForElement:statusElement] intValue];

    TBXMLElement *votesCountElement = [TBXML childElementNamed:@"v" parentElement:element];
    int votesCount = [[TBXML textForElement:votesCountElement] intValue];    

    // Creating the Request custom object
    Request *request = [[Request alloc] init];

    request.requestId = requestId;
    request.description = description;
    request.status = status;
    request.votes_count = votesCount;

    [requestsArray addObject:request];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    appListCell *cell = (appListCell *)[tableView dequeueReusableCellWithIdentifier:@"appListCell"];

    Request *request = [requestsArray objectAtIndex:indexPath.row];

    cell.appIdLabel.text = [NSString stringWithFormat:@"#%d", request.requestId];
    cell.appTextLabel.text = request.description;
    cell.appVotesLabel.text = [NSString stringWithFormat:@"%d votes", request.votes_count];

    return cell;
}

どうもありがとう!

編集:

- (void)traverseXMLAppRequests:(TBXMLElement *)element {
    int requestCount = 0;
    TBXMLElement *requestElement = element->firstChild;

    // Do we have elements inside <re>?
    if ((requestElement = (element->firstChild))) {
        while (requestElement) {
            [self createRequestFromXMLElement:requestElement];

            requestCount++;

            requestElement = [TBXML nextSiblingNamed:@"r" searchFromElement:requestElement];
        }
    }

    [self.tableView reloadData];   

    NSLog(@"Ok, I'm done. %d objects in the array.", [requestsArray count]);

}

編集 2: 20 行ではなく、1 行だけの情報を取得しようとしましたが、遅延はまったく同じです。

4

3 に答える 3

8

reloadData以外のスレッドで呼び出していますmainThread

tableView以下のように、必ずメイン スレッドで のデータをリロードしてください。

OBJ-C:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

スウィフト 3:

DispatchQueue.main.async {
   self.tableView.reloadData()
}
于 2015-05-26T10:34:54.467 に答える
0

どこでやる:

- (void)createRequestFromXMLElement:(TBXMLElement *)element

呼ばれる?そのファイルの内容を取得するために20のWebリクエストを行っているようです。それは間違いなく物事を遅くします(XMLファイル全体を取得するために1つのリクエストを実行し、それをローカルで解析するのとは対照的です)。

VCコード全体を投稿できますか?

于 2012-03-19T20:31:43.123 に答える