如何在Objective-C中使用NSOperation和NSOperationQueue?

kav*_*avi 7 objective-c nsoperation nsoperationqueue

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

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

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

使用两者将在缓慢的过程中显示图像.我的UI在显示图像方面没有那么好.

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

我对NSOperation知之甚少.我在谷歌搜索,我只是得到快速的教程,而我需要帮助Objective C.

所以,请任何人都可以解释NSOperationNSOperationQueue举例?

Jam*_*lam 6

将其应用于每个工作:

        // 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 */

        }];
Run Code Online (Sandbox Code Playgroud)

您也可以不使用NSOperationQueue进行申请:

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

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


            });
        });
Run Code Online (Sandbox Code Playgroud)

尝试更多:

  1. NSOperationQueue addOperationWithBlock返回到mainQueue的操作顺序

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

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