您的位置:首页 > 移动开发 > IOS开发

iOS某操作霸占主线程过久导致界面假死的一种解决方法

2015-12-02 16:14 381 查看
转载请注明出处:iOS某操作霸占主线程过久导致界面假死的一种解决方法
- 博客园

/*首先往消息中心注册一个检测针对的observer。*/
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handleresult:) name:@"postno." object:nil];

/*然后创建一个线程做之前耗时过久的操作。*/
[nsthread detachnewthreadselecto:@selector(calculate) totarget:self withobject:nil];

/*耗时的操作*/
- (void)caculate
{
/*新建的线程必须创建自己的内存释放池!*/
nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];
nsstring *result = @"1111";
/*事情做完后告知消息中心*/
[[nsnotificationcenter defaultcenter] postnotificationname:"postno." object:result];
[pool release];
}
/*耗时操作执行完后的回调函数*/
- (void)handleresult:(nsnotification *)noti
{
id obj = [noti object];
[self performselectoronmainthread:@selector(getresult:) withobject:obj waitutildone:yes];
}
/*在主线程进行的操作*/
- (void)getresult:(id)result
{
//do some thing(更新ui界面之类的)
}


记得结束后,把observer从nsnotificationcenter 中remover掉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: