NSOperationQueue addOperationWithBlock 返回mainQueue的操作顺序 [英] NSOperationQueue addOperationWithBlock return to mainQueue order of operations

查看:92
本文介绍了NSOperationQueue addOperationWithBlock 返回mainQueue的操作顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于操作顺序如何在 NSOperationQueue addOperationsWithBlock 中的基本问题.

I have a basic question as to how the order of operations goes inside NSOperationQueue addOperationsWithBlock.

我想做什么:

[_queue addOperationWithBlock:^{
    //starts a new thread to download an image
    //returns a response from the server
    if (returnResponse != nil) {
        //what I need to accomplish:
        //1) Update the collectionViewCell with data
        //2) Cache the image locally
        //3) Save the image name in local database
    }
}];

我不需要代码来做这件事,我只需要知道它是如何工作的.例如,如果我想立即为用户更新单元格,我是否应该像这样立即获得那部分代码?

I don't need the code to do this, I just need to know how it works. For example, if I want to update the cell right away for the user, should I have that part of code right away like this?

if (returnResponse != nil) {
    [[NSOperationQueue mainQueue]addOperationWithBlock^{
        //update the cell content on the main Thread
    }];
    //write data to file, save image name in local database
}

我的主要问题是:这样做,缓存图像并保存在本地数据库中是否会在用于下载图像的那个单独线程上完成?如果我颠倒顺序(缓存图像,保存在本地数据库中,然后更新单元格),会有什么不同吗?

My main question is: doing it this way, will caching the image and saving in local database be done on that separate thread, the one that was used to download the image? And if I reverse the order (cache the image, save in local database, then update the cell), will that make any difference?

解决方案:

在尝试了许多不同的方法之后,我在 NSOperationQueue mainQueue 中使用了串行 GCD.尝试保存在 sqlite DB 中一直给我一个 database is locked 错误,即使我正确地完成并关闭了数据库.我认为是因为它试图同时保存,在另一个查询试图访问它的同时打开一个数据库.所以对我来说最终的解决方案是这样的:

After trying many different ways, I went with serial GCD inside NSOperationQueue mainQueue. Trying to save in sqlite DB kept giving me a database is locked error, even though I finalized and closed the databases correctly. I think because it was trying to save concurrently, leaving one database open at the same time another query was trying to access it. So the final solution for me looks like this:

[_queue addOperationWithBlock:^{
    //starts a new thread to download an image
    //returns a response from the server
    if (returnResponse != nil) {
        //Cache the image locally
        [[NSOperationQueue mainQueue]addOperationWithBlock^{
            //use a created GCD serial queue to save the image name in local database
            //update the cell content on the main Thread
        }];
    }
}];

推荐答案

为什么不将 GCD 与并发队列一起使用?您可以执行以下操作:

Why don't you use GCD with concurrent queues? You could do something like the following:

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//execute first
            dispatch_sync(concurrentQueue, blockOfCode);
//execute second
            dispatch_sync(concurrentQueue, blockOfCode);
//execute third: update the UI
dispatch_sync(dispatch_get_main_queue(), blockOfCodeToUpdateUI);

这篇关于NSOperationQueue addOperationWithBlock 返回mainQueue的操作顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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