gpt4 book ai didi

iphone - bool _WebTryThreadLock(bool) 与 UITableView 崩溃

转载 作者:行者123 更新时间:2023-11-28 17:59:44 25 4
gpt4 key购买 nike

我正在尝试在 UITableView 中显示推文。

代码:-

- (void)requestTimeline:(int)count
{
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"abc" forKey:@"screen_name"];
[parameters setObject:@"20" forKey:@"count"];
[parameters setObject:@"1" forKey:@"include_entities"];

TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];

[request performRequestWithHandler:^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (responseData1 != nil)
{
NSError *error = nil;
self.itemsToDisplay = [NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableLeaves error:&error];

if (self.itemsToDisplay != nil)
{
[self.tableView reloadData];
}
else
{
NSLog(@"Error serializing response data %@ with user info %@.", error, error.userInfo);
}
}
else
{
NSLog(@"Error requesting timeline %@ with user info %@.", error, error.userInfo);
}
}];
}

selfUITableViewController 的子类。

由于阻塞,twitter 请求将在另一个线程而不是主线程上执行。这就是它崩溃的原因。

//TWRequest.h

// Issue the request. This block is not guaranteed to be called on any particular thread.
- (void)performRequestWithHandler:(TWRequestHandler)handler;

崩溃报告:-

bool _WebTryThreadLock(bool), 0xa8e4f50: 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...

如何解决这个问题?

谢谢...

最佳答案

Web View 只能在主线程中使用。大部分 UIKit —— tableView reloadData 命令也不是线程安全的

dispatch_async(dispatch_get_main_queue(), ^{ 
//all you ever do with UIKit.. in your case the reloadData call
[self.tableView reloadData];
});

关于iphone - bool _WebTryThreadLock(bool) 与 UITableView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14136618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com