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

ios并发编程的总结

2013-05-24 11:09 357 查看
最近这段时间,花了很多精力,仔细的学习了ios并发编程相关的内容,对并发编程的理解有了更深刻、全面的认识,现做一个简单的总结。

一,ios中实现并发有哪几种方式?

GCD,NSQueueOperation,NSTimer,NSThread。

二,上面四种实现方式,具体是有哪些常用的接口及注意事项:

1,GCD

dispatch_queue_t currentQueue = dispatch_get_globle_queue(DISPATCH_QUEUE_PRIORITY,0);

dispath_async(currentQueue,^(void)

{

NSLog(@"do something");

});

dispatch_get_main_queue();

dispatch_once_t

dispatch_sync()

dispatch_after(); //需要注意,以纳秒为单位

dispatch_groupt taskGroup = dispatch_group_create();

dispatch_group_async(taskGroup,mainQueue,^(void)

{

NSLog(@"do something");

})

dispatch_group_notify(taskGroup,mainQueue,^{

NSLog(@"finish");

})

dispatch_release(taskGroup);

2,NSTimer

//稍后继续。

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats //这是runloop自动管理的。

如果生成了timer的实例,在退出(释放掉)当前controller之前,应该将timer invalidate掉。(注意,在dealloc中调用invalidate是无效的)

3,NSThread

detachNewThreadSelector:target:withObject:

performSelectorInBackground

在以前的sdk,中方法必须加上@autoreleasepool才能编译通过,但是我实验了一下,在xcode 4.6中,不加也能正常运行,ios 5 programing cocabook 上说的也不一定全部都是对的。

ok,基本总结完成了,希望能对大家理解ios并发编程的理解有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: