1

基本的に同じことを行う2つのプログラムがあります。XML フィードを読み取り、要素を解析します。どちらのプログラムも、非同期 NSURLConnection を使用してデータを取得し、新しいスレッドを生成して解析を処理するように設計されています。5 つのアイテムのバッチが解析されると、メイン スレッドにコールバックして UITableView をリロードします。

私の問題は、あるプログラムでは正常に機能しますが、他のプログラムでは機能しないことです。解析が実際にはバックグラウンド スレッドで行われていることはわかっています。また、[tableView reloadData] がメイン スレッドで実行されていることもわかっています。ただし、すべての解析が完了するまでテーブルをリロードしません。私は困惑しています。私が知る限り...両方のプログラムはまったく同じように構成されています。正しく動作しないアプリのコードを次に示します。

- (void)startConnectionWithURL:(NSString *)feedURL feedList:(NSMutableArray *)list {
self.feedList = list;

// Use NSURLConnection to asynchronously download the data. This means the main thread will not be blocked - the
// application will remain responsive to the user. 
//
// IMPORTANT! The main thread of the application should never be blocked! Also, avoid synchronous network access on any thread.
//
NSURLRequest *feedURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURL]];
self.bloggerFeedConnection = [[[NSURLConnection alloc] initWithRequest:feedURLRequest delegate:self] autorelease];

// Test the validity of the connection object. The most likely reason for the connection object to be nil is a malformed
// URL, which is a programmatic error easily detected during development. If the URL is more dynamic, then you should
// implement a more flexible validation technique, and be able to both recover from errors and communicate problems
// to the user in an unobtrusive manner.
NSAssert(self.bloggerFeedConnection != nil, @"Failure to create URL connection.");

// Start the status bar network activity indicator. We'll turn it off when the connection finishes or experiences an error.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.bloggerData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [bloggerData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.bloggerFeedConnection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
// Spawn a thread to fetch the link data so that the UI is not blocked while the application parses the XML data.
//
// IMPORTANT! - Don't access UIKit objects on secondary threads.
//
[NSThread detachNewThreadSelector:@selector(parseFeedData:) toTarget:self withObject:bloggerData];
// farkData will be retained by the thread until parseFarkData: has finished executing, so we no longer need
// a reference to it in the main thread.
self.bloggerData = nil;
}

これを上から下に読むと、NSURLConnection がいつ終了するかがわかります。新しいスレッドを切り離して、parseFeedData を呼び出します。

- (void)parseFeedData:(NSData *)data {
// You must create a autorelease pool for all secondary threads.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

self.currentParseBatch = [NSMutableArray array];
self.currentParsedCharacterData = [NSMutableString string];
self.feedList = [NSMutableArray array];
//
// It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
// because it gives less control over the network, particularly in responding to connection errors.
//
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];

// depending on the total number of links parsed, the last batch might not have been a "full" batch, and thus
// not been part of the regular batch transfer. So, we check the count of the array and, if necessary, send it to the main thread.
if ([self.currentParseBatch count] > 0) {
    [self performSelectorOnMainThread:@selector(addLinksToList:) withObject:self.currentParseBatch waitUntilDone:NO];
}
self.currentParseBatch = nil;
self.currentParsedCharacterData = nil;
[parser release];        
[pool release];
}

did end 要素デリゲートでは、メイン スレッドを呼び出して更新を実行する前に、5 つの項目が解析されていることを確認します。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:kItemElementName]) {
    [self.currentParseBatch addObject:self.currentItem];
    parsedItemsCounter++;
    if (parsedItemsCounter % kSizeOfItemBatch == 0) {
        [self performSelectorOnMainThread:@selector(addLinksToList:) withObject:self.currentParseBatch waitUntilDone:NO];
        self.currentParseBatch = [NSMutableArray array]; 
    }
}  

// Stop accumulating parsed character data. We won't start again until specific elements begin.
accumulatingParsedCharacterData = NO;
}

- (void)addLinksToList:(NSMutableArray *)links {
[self.feedList addObjectsFromArray:links];
// The table needs to be reloaded to reflect the new content of the list.

if (self.viewDelegate != nil && [self.viewDelegate respondsToSelector:@selector(parser:didParseBatch:)]) {
    [self.viewDelegate parser:self didParseBatch:links];
}

}

最後に、UIViewController デリゲート:

- (void)parser:(XMLFeedParser *)parser didParseBatch:(NSMutableArray *)parsedBatch {
NSLog(@"parser:didParseBatch:");
[self.selectedBlogger.feedList addObjectsFromArray:parsedBatch];
[self.tableView reloadData];

}

テーブルをリロードするためにビュー コントローラー デリゲートが起動したとき、およびテーブルの再構築中に cellForRowAtIndexPath が起動したときにログに書き込むと、ログは次のようになります。

parser:didParseBatch:
parser:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
parser:didParseBatch:
parser:didParseBatch:
parser:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
パーサー:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
パーサー:didParseBatch:
パーサー:didParseBatch:
パーサー:didParseBatch:
パーサー:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath

明らかに、tableView は毎回指示してもリロードされません。

正しく動作するアプリのログは次のようになります。

parser:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
parser:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
parser:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView: cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
パーサー:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
パーサー:didParseBatch:
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath

4

1 に答える 1

0

あなたのデータモデルの何かが原因であると推測します。ほとんどの場合、セクション番号が間違っているか、セクションの行がゼロであると報告されていると思います。

テーブルは、を呼び出す前にと の両方– numberOfSectionsInTableView:を呼び出します。それらのいずれかが間違った番号を報告した場合、テーブルビューはデータがないと見なし、何もしません。 – tableView:numberOfRowsInSection:- tableView:cellForRowAtIndexPath

于 2010-01-20T20:36:21.473 に答える