iPhone 上的 NSOperation

发布于 2024-07-19 13:12:48 字数 217 浏览 2 评论 0原文

我一直在寻找一些具体场景,说明 iPhone 上的 NSOperation 何时成为应用程序中使用的理想工具。 据我了解,这是编写您自己的线程代码的包装器。 我还没有看到任何 Apple 演示应用程序使用它,我想知道我是否错过了一个很棒的工具,而不是使用 NSThread。

这里理想的解决方案是描述 NSOperation 的用例场景以及如何使用它来解决您的问题。

I've been looking for some concrete scenarios for when NSOperation on the iPhone is an ideal tool to use in an application. To my understanding, this is a wrapper around writing your own threaded code. 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.

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独行侠 2024-07-26 13:12:50

这只是一个非常简单的实现,但需要时间阅读教程才能完全理解所有内容:

NSOperationQueue *queue = [NSOperationQueue new];

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

[queue addOperation:operation];

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];
记忆之渊 2024-07-26 13:12:50

您可以尝试使用 Swift 的示例

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

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

A sample that you can try using Swift

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

let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(operation)
睡美人的小仙女 2024-07-26 13:12:50

我用它来进行异步处理。 这是从 Web 服务获取数据或协调需要大量时间运行的操作的最佳方式。 因为它们是线程安全的、异步的(不占用主线程)并且支持依赖项,所以它们对于您的工具集来说是一个非常好的工具。

依赖关系允许您执行多个单独的操作,并确保按特定顺序执行并成功或出错。 当您需要同步一堆数据但需要在同步子对象之前先同步父对象时,这确实很棒。

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. 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.

不寐倦长更 2024-07-26 13:12:49

您还应该查看此网址:
http://developer.apple.com/cocoa/managingconcurrency.html

以上所有答案很棒,但请确保您阅读了上面的文章,并在代码中自由使用了这一行:

if ( self.isCancelled ) return;

Coca is my Girlfriend 提供的示例中没有使用该行,直到我从我意识到这是一个问题/概念的领域。

You should also check out this 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.

已下线请稍等 2024-07-26 13:12:49

简而言之: NSOperationQueue

是线程安全的(您可以从不同的线程向其添加操作,而不需要锁),并且使您能够将 NSOp 对象链接在一起。

我的 Flickr iPhone 应用程序 Reflections 广泛使用 NSOperationNSOperationQueue 来管理下载图像和 XML

注意:确保您阅读、重读并理解文档中谈论“并发”时的含义。

In a word: 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.

My Flickr iPhone app, Reflections, uses NSOperation and NSOperationQueue extensively to manage downloading images and XML.

Caveat: Make sure you read, re-read, and understand what the docs mean when they talk about 'concurrency'.

邮友 2024-07-26 13:12:49

我在 iPhone 应用程序中使用它的方式基本上是在我的应用程序委托中创建一个 NSOperationQueue 成员,并通过属性使其可用。 然后,每次我需要在后台运行某些内容(例如下载一些 XML)时,我都会创建一个 NSInitationOperation 并将其发送到队列。

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

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. Then every time I need to run something in the background, e.g. download some XML I'll just create an NSInvocationOperation and send it to the queque.

NSInvocationOperation *operationToPerform = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateXML) object:nil];
[[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:operationToPerform];
[op release];
往日 2024-07-26 13:12:49

Cocoa Is My Girlfriend 有一个很好的教程关于 NSOperation 和 NSOperationQueue 的使用。 本教程使用 NSOperation 在单独的线程中同时下载多个网页。

另请参阅 Mac Research 的这篇文章

Cocoa Is My Girlfriend has a good tutorial on the use of NSOperation and NSOperationQueue. The tutorial makes use of NSOperation to download several webpages simultaneously in separate threads.

Also, see this article from Mac Research.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文