使用NSOperationQueue同时执行两个后台任务 [英] Two simultaneous background tasks using NSOperationQueue

查看:431
本文介绍了使用NSOperationQueue同时执行两个后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个任务需要在后台同时执行不同的线程优先级:

I have two tasks that i need to perform in background simultaneously with different thread priorities:


  1. 不断收集和处理陀螺仪动作数据(高优先级)

  1. continuously gather and process gyro motion data (high priority)

gyroQueue = [[NSOperationQueue alloc] init];

[self.motionManager startDeviceMotionUpdatesToQueue:gyroQueue withHandler:^(CMDeviceMotion *motion, NSError *error){
    [self processMotion:motion withError:error];
}];


  • 有时会处理图像而不会干扰动画更新(转换,裁剪,缩小等等= 1 -2sec)(非常低的优先级)

  • Sometimes process images without interfering with motion updates (convert, crop, sesize, etc = 1-2sec) (very low priority)

    imageProcessingQueue = [[NSOperationQueue alloc] init];
    
    [imageProcessingQueue addOperationWithBlock:^{
        [self processImage:[UIImage imageWithData:imageData]];
    }];
    


  • 编辑:这就是我所拥有的最初(而不是块操作),它仍然阻止运动更新:

    this is what I had in place originally (instead of block operation), and it is still blocking the motion updates:

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(processImage:) object:[UIImage imageWithData:imageData]];
    [operation setThreadPriority:0.0];
    [operation setQueuePriority:0.0];
    [imageProcessingQueue addOperation:operation];
    

    似乎这两个任务都在同一个后台线程上执行(由于NSOperationQueue的性质? ),处理图像会阻止gyroQueue更新,直到完成,这就是我要避免的。

    It seems as both of these tasks are being executed on the same background thread (due to NSOperationQueue nature?), and processing of an image blocks the gyroQueue updates until it's done, which is what i am trying to avoid.

    如何使用NSOperationQueue生成两个单独的线程,并且分配非常高&相应的优先级如何?

    How can I spawn two separate threads using NSOperationQueue, and assign veryHigh & veryLow priorities accordingly?

    编辑:这个问题仍然是开放的,我正在使用Travor Harmon的图像调整大小功能来调整图像大小。有人可以确认它是否是线程安全的吗?

    this question is still open, i am using Travor Harmon's image resize functions for image resize. Can someone confirm if it's thread safe?

    推荐答案

    我找到了解决此问题的解决方案(或可靠的解决方法):

    i have found a solution (or solid workaround) for this issue:

    而不是使用 startDeviceMotionUpdatesToQueue 将deviceMotion更新到队列,我创建了一个CADisplayLink计时器,它没有干扰其他背景队列 - 当它匹配屏幕刷新率时,它的性质给予最高优先级:

    instead of routing deviceMotion updates to the queue using startDeviceMotionUpdatesToQueue, i have created a CADisplayLink timer and it is not interfering with other background queues - while it is matching screen refresh rate it's given highest priority by it's nature:

    [self.motionManager startDeviceMotionUpdates];
    
    gyroTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(processMotion)];
    [gyroTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    

    这篇关于使用NSOperationQueue同时执行两个后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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