如何在 NSOperation 中运行异步进程
我需要通过我正在开发的应用程序中的后台线程更新网络服务中的一些数据。
通常,我只是通过 NSOperationQueue 和 NSOperation 的 main() 函数中的同步 Web 请求来完成此操作。
但是,对于此特定数据获取,应用程序需要使用 OAuth
和我正在使用的 OAuthConsumer
库,通过带有委托方法回调的异步请求执行对 Web 服务的请求对于成功/失败。
我的问题是,那些完成的委托回调似乎得到了进程,因为(据我的理解)当到达 main()
函数末尾并且操作完成时,我的操作进程被销毁/清理。从操作队列的堆栈中弹出。
这准确吗?
如果是这样,是否有解决方案可以通过 NSOperationQueue 执行此操作?如果没有,当前通过后台线程以异步方式获取数据(可以处理委托回调)的最佳实践是什么?
I have a need to update some data from a webservice via a background thread in an app I'm working on.
Normally I'd just do this via an NSOperationQueue
and a synchronous web request in the main() function of the NSOperation
.
However, for this specific data fetch, the app requires the use of OAuth
and the OAuthConsumer
library I am using performs the request to the webservice via an asynchronous request with delegate method callbacks for success/failure.
My issue is that those delegate callbacks done seem to get processes, because (as is my understanding) my operation process is destroyed/cleaned up when the end of the main()
function is reached and the operation is pop off the operation queue's stack.
Is this accurate?
If so, is there a solution to doing this via an NSOperationQueue and if not, what is the current best practice for grabbing data via a background thread in an asynchronous manner that can handle delegate callbacks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以继续运行循环,直到操作完成。您要做的就是启动异步操作并继续运行运行循环,直到它们完成。完整标志将是一个实例变量,您将在所有异步 (OAuth) 项目完成时设置该变量。
You can continue the run loop until your operations are complete. What you will do is start you asynchronous operations and the keep running the run loop until they are complete. The complete flag will be an instance variable that you will set when all of your async (OAuth) items are done.