【解决】iOS开发,更新界面时崩溃,Tried to obtain the web lock from a thread other than the main thread or ……
2012-09-24 11:52
621 查看
在开发一些涉及网络访问的 app 时,我们通常的逻辑是将耗时网络操作放在新起的线程中去做,让在主线程中执行的方法尽快返回,否则前台视图就会处于假死状态。等后台线程的网络访问结束时再去更新前台视图。不过这样的逻辑在iOS开发中遇到了问题。
我的 app 需要用户登陆,当用户点击登陆按钮后我新起一个线程去服务器验证,验证成功后跳转到新的视图。如下:
【点击登陆按钮】→【新起线程访问服务器】→【成功后跳转视图】
跳转视图的代码:
[self.navigationController pushViewController:shakeViewCtr animated:YES];
我用的是导航控制器,shakeViewCtr 是将要跳转到的视图,然后程序崩溃,控制台出现如下错误提示:
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...
提示说是因为在副线程中调用了 UIKIT 的方法,UIKIT 是 iOS 的 UI 框架。这就意味着在非主线程中不能更改前台视图。
别急,苹果还有这样的机制来处理,可以在副线程中通知主线程去更改前台视图。
将更新视图的代码用一个方法(loginSuccess)来封装,副线程中完成网络访问后使用 performSelectorOnMainThread:withObject:waitUntilDone 方法来通知主线程执行loginSuccess 方法。部分代码如下:
[self performSelectorOnMainThread:@selector(loginSuccess) withObject:nil waitUntilDone:NO];
- (void)loginSuccess {
[self.navigationController pushViewController:shakeViewCtr animated:YES];
}
那么,现在的流程是:
【点击登陆按钮】→【新起线程访问服务器】→【成功后通知主线程跳转视图】
参考:http://zyc-to.blog.163.com/blog/static/171524002011997143682/
我的 app 需要用户登陆,当用户点击登陆按钮后我新起一个线程去服务器验证,验证成功后跳转到新的视图。如下:
【点击登陆按钮】→【新起线程访问服务器】→【成功后跳转视图】
跳转视图的代码:
[self.navigationController pushViewController:shakeViewCtr animated:YES];
我用的是导航控制器,shakeViewCtr 是将要跳转到的视图,然后程序崩溃,控制台出现如下错误提示:
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...
提示说是因为在副线程中调用了 UIKIT 的方法,UIKIT 是 iOS 的 UI 框架。这就意味着在非主线程中不能更改前台视图。
别急,苹果还有这样的机制来处理,可以在副线程中通知主线程去更改前台视图。
将更新视图的代码用一个方法(loginSuccess)来封装,副线程中完成网络访问后使用 performSelectorOnMainThread:withObject:waitUntilDone 方法来通知主线程执行loginSuccess 方法。部分代码如下:
[self performSelectorOnMainThread:@selector(loginSuccess) withObject:nil waitUntilDone:NO];
- (void)loginSuccess {
[self.navigationController pushViewController:shakeViewCtr animated:YES];
}
那么,现在的流程是:
【点击登陆按钮】→【新起线程访问服务器】→【成功后通知主线程跳转视图】
参考:http://zyc-to.blog.163.com/blog/static/171524002011997143682/
相关文章推荐
- Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be
- Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be
- Tried to obtain the web lock from a thread解决方法之一
- Tried to obtain the web lock from a thread解决方法之一
- 0xab7dcd0: Tried to obtain the web lock from a thread
- 芒果iOS开发之This application is modifying the autolayout engine from a background thread崩溃
- iOS开发之使用CocoaPods更新第三方出现“target overrides the `OTHER_LDFLAGS`……”问题解决方案
- 芒果iOS开发之This application is modifying the autolayout engine from a background thread崩溃
- iOS开发之使用CocoaPods更新第三方出现“target overrides the `OTHER_LDFLAGS`……”问题解决方案
- Tomcat常见问题解决 - The web application appears to have started a thread named [FileWatchdog]
- iOS ERROR: unable to get the receiver data from the DB 解决方式
- iOS开发79-Xcode报错:The file “XXX” couldn’t be opened because you don’t have permission to view it.的解决方法
- https使用HttpWebRequest出现错误:System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.的解决方法
- The Webpage might be temporarily down or it may have moved permanently to a new web address解决
- iOS开发问题---调试提示The certificate used to sign “appName” has either expired or has been revoked.
- When you copy large files to or from earlier operating systems, the copy operation may be slower than expected on some Windows Vista-based computers
- 关于MBP 加载菊花 崩溃到 MBP.m 报: MBProgressHUD needs to be accessed on the main thread.
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- reason: '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance --iOS开发之崩溃
- 【转发】Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on