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 a result of calling to UIKit from a secondary thread”,一旦出现这个错误,程序会立即crashed。

这是个非常著名的BUG,apple不允许程序员在主线程以外的线程中对ui进行操作.

有很多情形下都会发生这个BUG, 比如,当时使用了NSOperation进行了http异步请求,然后使用kvo模式注册观察者,当数据下载完毕后,在主线程中接收下载完毕的通知,并在observeValueForKeyPath方法中使用[tableview reloadData]更新UI。

这样也导致了上述错误。解决的方法是使用performSelectorOnMainThread进行ui的更新:

[self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];


拓展知识:

关于performSelectorOnMainThread:的介绍

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];

       [selfperformSelectorOnMainThread:@selector(RefreshCellForLiveId:)

                          withObject:userinfo

                        waitUntilDone:YES];

       [pool release];


该方法的作用是在主线程中,执行制定的方法(代码块)。

参数:

@selector(RefreshCellForLiveId:)就是,要定义我们要执行的方法。

withObject:userinfo

定义了,我们执行RefreshCellForLiveId:方法时,传入的参数对象。类型是id。(我们可以传入任何参数)

waitUntilDone:YES];

指定,当前线程是否要被阻塞,直到主线程将我们制定的代码块(RefreshCellForLiveId:方法)执行完。


注意:

1.当前线程为主线程的时候,waitUntilDone:YES参数无效。

2.该方法,没有返回值

3.该方法主要用来用主线程来修改页面UI的状态。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值