NSOperation和NSOperationQueue工作线程vs主线程 [英] NSOperation and NSOperationQueue working thread vs main thread

查看:162
本文介绍了NSOperation和NSOperationQueue工作线程vs主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的应用程序中执行一系列下载和数据库写入操作。我正在使用 NSOperation NSOperationQueue

I have to carry out a series of download and database write operations in my app. I am using the NSOperation and NSOperationQueue for the same.

这是应用场景:


  • 从一个地方获取所有邮政编码。

  • 为每个邮政编码提取所有房屋。

  • 每个房屋取得居民的详细信息

如上所述,我为每项任务定义了 NSOperation 。在第一种情况下(Task1),我向服务器发送请求以获取所有邮政编码。 NSOperation 中的代表将收到数据。然后将此数据写入数据库。数据库操作在不同的类中定义。从 NSOperation 类我正在调用数据库类中定义的write函数。

As said, I have defined an NSOperation for each task. In first case (Task1), I am sending a request to server to fetch all postcodes. The delegate within the NSOperation will receive the data. This data is then written to database. The database operation is defined in a different class. From NSOperation class I am making a call to the write function defined in database class.

我的问题是,是否数据库写操作发生在主线程或后台线程中?当我在 NSOperation 中调用它时,我希望它在另一个线程(Not MainThread)中运行,因为 NSOperation 。有人可以在处理 NSOperation NSOperationQueue 时解释这种情况。

My question is whether the database write operation occur in main thread or in a background thread? As I was calling it within a NSOperation I was expecting it to run in a different thread (Not MainThread) as the NSOperation. Can someone please explain this scenario while dealing with NSOperation and NSOperationQueue.

推荐答案


我的问题是数据库写操作是在主
线程还是后台线程中发生的?

My question is whether the database write operation occur in main thread or in a background thread?

如果您从头开始创建 NSOperationQueue ,如下所示:

If you create an NSOperationQueue from scratch as in:

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];

它将在后台线程中:

操作队列通常提供用于运行
操作的线程。在OS X v10.6及更高版本中,操作队列使用
libdispatch库(也称为Grand Central Dispatch)来启动
执行其操作。 因此,操作总是在单独的线程上执行
,无论它们是否被指定为并发或非并发操作的

Operation queues usually provide the threads used to run their operations. In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as concurrent or non-concurrent operations

除非您使用 mainQueue

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

您还可以看到如下代码:

You can also see code like this:

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
[myQueue addOperationWithBlock:^{

   // Background work

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // Main thread work (UI usually)
    }];
}];

GCD版本:

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
             {
              // Background work            
             dispatch_async(dispatch_get_main_queue(), ^(void)
              {
                   // Main thread work (UI usually)                          
              });
});

NSOperationQueue 可以更好地控制你想要的东西去做。您可以在两个操作之间创建依赖关系(下载并保存到数据库)。要在一个块和另一个块之间传递数据,您可以假设,例如, NSData 将来自服务器,因此:

NSOperationQueue gives finer control with what you want to do. You can create dependencies between the two operations (download and save to database). To pass the data between one block and the other, you can assume for example, that a NSData will be coming from the server so:

__block NSData *dataFromServer = nil;
NSBlockOperation *downloadOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakDownloadOperation = downloadOperation;

[weakDownloadOperation addExecutionBlock:^{
 // Download your stuff  
 // Finally put it on the right place: 
 dataFromServer = ....
 }];

NSBlockOperation *saveToDataBaseOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakSaveToDataBaseOperation = saveToDataBaseOperation;

 [weakSaveToDataBaseOperation addExecutionBlock:^{
 // Work with your NSData instance
 // Save your stuff
 }];

[saveToDataBaseOperation addDependency:downloadOperation];

[myQueue addOperation:saveToDataBaseOperation];
[myQueue addOperation:downloadOperation];

编辑:为什么我使用 __弱操作参考,可以在这里找到。但简而言之就是避免保留周期。

Why I am using __weak reference for the Operations, can be found here. But in a nutshell is to avoid retain cycles.

这篇关于NSOperation和NSOperationQueue工作线程vs主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆