【问题标题】:How to use NSOperation & NSOperationQueue in Objective-C?如何在 Objective-C 中使用 NSOperation 和 NSOperationQueue?
【发布时间】:2017-02-18 13:09:57
【问题描述】:

我正在开发一个自定义相机应用程序。我正在做的是,我正在使用相机拍照并将它们显示在屏幕的同一 VC 底部。

我将图像存储在本地字典和 NSDocument 目录路径中。如果图片在本地字典中,它将从本地字典中获取,否则将从 NSDocument 目录路径中获取。

收到内存警告后,我只是将字典归零,所以它会从 NSDocument 目录路径中获取图像。

同时使用两者会在缓慢的过程中显示图像。我的用户界面在显示图像方面不太好。

所以我想使用 NSOperation 将图像存储在 NSDocument 目录路径中。

我对 NSOperation 了解不多。我在 google 中搜索过,我只是得到了 swift 教程,而我在 Objective C 中需要帮助。

那么请任何人解释一下NSOperationNSOperationQueue 的例子吗?

【问题讨论】:

  • 教程和其他场外资源的请求在这里是题外话。为了避免关闭您的问题,我已从您的问题中删除了题外话的要求。如果你愿意,你可以随时回滚,但如果你这样做,社区将不得不关闭它

标签: objective-c nsoperation nsoperationqueue


【解决方案1】:

Swift3 创建操作队列

lazy var imgSaveQueue: OperationQueue = {
    var queue = OperationQueue()
    queue.name = "Image Save Queue"
    queue.maxConcurrentOperationCount = 1
    return queue
}()

给它添加操作

imgSaveQueue.addOperation(BlockOperation(block: { 
       //your image saving code here 
    }))

对于目标 C:

[[NSOperationQueue new] addOperationWithBlock:^{ 

      //code here 

}];

【讨论】:

  • 谢谢@Hunaid Hassan,我在objectivec中需要它,请分享那个
  • [[NSOperationQueue new] addOperationWithBlock:^{ //code here }]
  • 谢谢@Hunaid Hassan,我需要一个完整的教程,请分享目标c的教程链接
  • @kavi 要求教程链接或任何场外资源是题外话
  • 为您添加了更多参考示例。看看这是否对你有帮助。 @kav
【解决方案2】:

将此应用于每项工作:

        // Allocated here for succinctness.
        NSOperationQueue *q = [[NSOperationQueue alloc] init];

        /* Data to process */
        NSData *data = [@"Hello, I'm a Block!" dataUsingEncoding: NSUTF8StringEncoding];

        /* Push an expensive computation to the operation queue, and then
         * display the response to the user on the main thread. */
        [q addOperationWithBlock: ^{
            /* Perform expensive processing with data on our background thread */
            NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];



            /* Inform the user of the result on the main thread, where it's safe to play with the UI. */

            /* We don't need to hold a string reference anymore */

        }];

你也可以在没有 NSOperationQueue 的情况下申请:

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // Your Background work

            dispatch_async(dispatch_get_main_queue(), ^{
                // Update your UI


            });
        });

试试这些:

  1. NSOperationQueue addOperationWithBlock return to mainQueue order of operations

  2. http://landonf.org/code/iphone/Using_Blocks_1.20090704.html

  3. https://videos.raywenderlich.com/courses/introducing-concurrency/lessons/7

【讨论】:

  • 好的。我修改了它。谢谢。请,如果您不赞成,请取消。 @NSNoob
  • 感谢 Jamshed Alam,实际上我将图像保存在 sigleton 类的 nsdocument 目录路径中,我必须在其中放置代码以保存字典。我必须将这两个代码放在我的单例类中吗?
  • 为每个图像应用一个 NSOperationQueue。所以代码将处于循环中。示例: for (;;) { NSOperationQueue *op...... 整个代码...... } 。试试看。。希望你能做到。如果做不到,请粘贴一些代码。
  • 我不知道谁反对它。你好有人,请在评论部分解释你为什么投反对票! .
  • 感谢 Jamshed Alam
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多