简体   繁体   English

NSOperation在iPhone上

[英]NSOperation on the iPhone

I've been looking for some concrete scenarios for when NSOperation on the iPhone is an ideal tool to use in an application. 我一直在寻找一些具体的场景,以确定iPhone上的NSOperation是否是在应用程序中使用的理想工具。 To my understanding, this is a wrapper around writing your own threaded code. 据我所知,这是编写自己的threaded代码的包装器。 I haven't seen any Apple demo apps using it, and I'm wondering if I'm missing out on a great tool instead of using NSThread . 我还没有看到任何使用它的Apple演示应用程序,我想知道我是否错过了一个很棒的工具,而不是使用NSThread

The ideal solution here would be to describe a use-case scenario for NSOperation and how you would use it to solve your problem(s). 这里理想的解决方案是描述NSOperation的用例场景以及如何使用它来解决您的问题。

Cocoa Is My Girlfriend has a good tutorial on the use of NSOperation and NSOperationQueue . Cocoa Is My Girlfriend有一个关于使用NSOperationNSOperationQueue好教程 The tutorial makes use of NSOperation to download several webpages simultaneously in separate threads. 本教程利用NSOperation在不同的线程中同时下载多个网页。

Also, see this article from Mac Research. 另外,请参阅Mac Research的这篇文章

The way I use it in my iPhone apps is to basically create an NSOperationQueue member in my application delegate and make it available through a property. 我在iPhone应用程序中使用它的方法是在我的应用程序委托中基本创建一个NSOperationQueue成员,并通过属性使其可用。 Then every time I need to run something in the background, eg download some XML I'll just create an NSInvocationOperation and send it to the queque. 然后每当我需要在后台运行某些东西时,例如下载一些XML,我只需创建一个NSInvocationOperation并将其发送到queque。

NSInvocationOperation *operationToPerform = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateXML) object:nil];
[[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:operationToPerform];
[op release];

In a word: NSOperationQueue 总之一句话: NSOperationQueue

NSOperationQueue is thread safe (you can add operations to it from different threads without the need for locks) and enables you to chain NSOp objects together. NSOperationQueue是线程安全的(您可以从不同的线程向其添加操作而无需锁定),并使您能够将NSOp对象链接在一起。

My Flickr iPhone app, Reflections, uses NSOperation and NSOperationQueue extensively to manage downloading images and XML . 我的Flickr iPhone应用程序Reflections,广泛使用NSOperationNSOperationQueue来管理下载图像和XML

Caveat: Make sure you read, re-read, and understand what the docs mean when they talk about 'concurrency'. 警告:确保您在阅读“并发”时阅读,重新阅读并理解文档的含义。

You should also check out this URL: http://developer.apple.com/cocoa/managingconcurrency.html 您还应该查看此URL: http//developer.apple.com/cocoa/managingconcurrency.html

All these above answers are great, but make sure you read the article above and make liberal use of this line in your code: 以上所有答案都很棒,但请确保您阅读上面的文章并在代码中自由使用此行:

if ( self.isCancelled ) return;

That line wasn't used in the samples provided by Coca is my Girlfriend, and it wasn't until I got crash logs in from the field that I realized this was an issue/concept. 在Coca提供的样本中没有使用该行是我的女朋友,直到我从现场获得崩溃登录才知道这是一个问题/概念。

Here is just a very simple implementation but take time to read the tutorials to fully understand everything: 这是一个非常简单的实现,但需要时间阅读教程以完全理解所有内容:

NSOperationQueue *queue = [NSOperationQueue new];

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
    selector:@selector(methodToCall)
    object:objectToPassToMethod];

[queue addOperation:operation];

I use it for asynchronous processing. 我用它进行异步处理。 It is the best way to get data from web services or to coordinate actions that take significant time to run. 这是从Web服务获取数据或协调需要大量时间运行的操作的最佳方法。 Because they are thread safe, asynchronous (doesn't tie up the main thread) and they support dependencies, they are a really great tool for your toolset. 因为它们是线程安全的,异步的(不占用主线程)并且它们支持依赖性,所以它们对于您的工具集来说是一个非常好的工具。

Dependencies allow you to make several separate operations and make sure the execute and succeed or error out in a certain order. 依赖关系允许您进行多个单独的操作,并确保以特定顺序执行并成功或错误输出。 This is really great when you need to synchronize a bunch of data but you need parent objects to sync before syncing child objects. 当您需要同步一堆数据但在同步子对象之前需要同步父对象时,这非常好。

A sample that you can try using Swift 您可以尝试使用Swift的示例

let operation : NSOperation = NSOperation()
operation.completionBlock = {
println("Completed")
}

let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(operation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM