cocoa - 使用 NSOperation main() 和 start() 方法中的哪一个

标签 cocoa cocoa-touch nsoperation

通过苹果文档

主要-

For non-concurrent operations, you typically override only one method:

main

开始-

When you add an operation to an operation queue, the queue ignores the value of the asynchronous property and always calls the start method from a separate thread. Therefore, if you always run operations by adding them to an operation queue, there is no reason to make them asynchronous.

如果我必须执行 Alamofire 的代码,它已经使用了操作队列并且是异步的。我应该在哪里编写代码(Main 或 Start)来调用 Alamofire 请求?

最佳答案

也许我没有捕获你答案的要点,但是,如果你需要将 Alamofire 包装在自定义操作中,则必须创建一个异步 NSOperation

您可以在this Github repo中找到一个很好的例子。 .

关键点是:

  • 覆盖返回 true异步属性
  • 覆盖start方法
  • 控制 isExecutingisFinished 属性以更改操作的状态

关于cocoa - 使用 NSOperation main() 和 start() 方法中的哪一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679635/

相关文章:

cocoa - launchd 守护进程的安全密码存储

cocoa - BeginSheet不显示窗口

objective-c - 深拷贝一个 NSArray

ios - 标题和第一个单元格之间的分隔——在普通的 UITableView 中

ios - 如何制作像 Facebook 应用程序一样的登录屏幕?

ios - NSOperation with NSTimer循环如何停止它

ios - 修复我的网络事件指示器

objective-c - 使用子类 NSView 关闭时执行操作

iphone - UITableView 的滚动背景图像?

swift - 由于 Swift 超时,如何取消 cloudKit 操作?