iOS开发关于NSOperationQueue

标签 ios cocoa nsoperation nsoperationqueue

我知道获取操作队列的两种方法如下:

queue1 = [[NSOperationQueue alloc] init];
queue2 = [[NSOperationQueue mainQueue] retain];

但我不知道它们之间有什么区别。

[queue1 addOperation:operation1]; 
[queue2 addOperation:operation2]; 

operation1 在哪个线程上运行?主线程?或不确定性?

我测试过。

operation1 --> sometimes mainthread sometimes not. 
operation2 --> always mainthread. 

最佳答案

According to the documentation NSOperationQueue 的:

create separate threads for non-concurrent operations and launch concurrent operations from the current thread.

这解释了为什么您的一些任务在主线程上运行而其他任务不运行。

mainQueue 绑定(bind)到主线程,所以操作总是在主线程上执行。

关于iOS开发关于NSOperationQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7736455/

相关文章:

ios - 升级后 Alamofire 不工作(Swift/Xcode 8)

ios - 从 NSSet 获取 NSManagedObject

objective-c - 从离屏 NSView 生成缩放图像

objective-c - 如何通过绑定(bind)和 NSArrayController 命名 Core Data 添加/删除项目的撤消菜单项?

ios - NSOperation 中委托(delegate)的使用

ios - 如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小)

iphone - TableViewCell 中的多列

objective-c - 如何在 Cocoa/WebKit 中 DOM 更改后自动滚动 WebView

iphone - iOS-移植C程序:在NSOperation作业中调用pthread_exit()

ios - 使用 HTTP 网络处理互联网连接变化的最佳方法