NSString *url = @"http://localhost/xml";
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:data error:&error];
int count = [[[xmlDictionary objectForKey:@"ArrayOfMessage"] objectForKey:@"Message"] count];
if(true && count > 5) count = 5;
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(58, 0, MESSAGE_PAGING_WIDTH, 493)];
[sv setBounces:YES];
[sv setClipsToBounds:NO];
[sv setPagingEnabled:YES];
[sv setShowsHorizontalScrollIndicator:FALSE];
[sv setContentSize:CGSizeMake(count * MESSAGE_PAGING_WIDTH, frame.size.height)];
int i = 0;
while (i < count) {
NSDictionary *data = [[[xmlDictionary objectForKey:@"ArrayOfMessage"] objectForKey:@"Message"] objectAtIndex:i];
[sv addSubview:[[MessageView alloc] initWithFrame:CGRectMake(i * MESSAGE_PAGING_WIDTH, 0, 838, 493) data:data]];
++i;
}
[self addSubview:sv];
}];
我收到了一条崩溃信息:
bool _WebTryThreadLock(bool), 0x7d7d080: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
我该如何补救?
答案 0 :(得分:2)
您在与主线程不同的线程上创建UIKit元素:
这可能是从辅助线程调用UIKit的结果。
sendAsynchronousRequest:queue:completionHandler:
在后台线程上执行。在块内,您需要对UIKit对象使用performSelectorOnMainThread:withObject:waitUntilDone:
。
请参阅http://blog.jayway.com/2010/03/30/performing-any-selector-on-the-main-thread/