【发布时间】:2013-04-08 06:25:00
【问题描述】:
我有两个任务需要在后台以不同的线程优先级同时执行:
-
不断收集和处理陀螺运动数据(高优先级)
gyroQueue = [[NSOperationQueue alloc] init]; [self.motionManager startDeviceMotionUpdatesToQueue:gyroQueue withHandler:^(CMDeviceMotion *motion, NSError *error){ [self processMotion:motion withError:error]; }]; -
有时在不干扰运动更新的情况下处理图像(转换、裁剪、调整大小等 = 1-2 秒)(非常低的优先级)
imageProcessingQueue = [[NSOperationQueue alloc] init]; [imageProcessingQueue addOperationWithBlock:^{ [self processImage:[UIImage imageWithData:imageData]]; }];
编辑:这是我最初所拥有的(而不是阻止操作),它仍然阻止运动更新:
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 更新,直到它完成,这是我试图避免的。
如何使用 NSOperationQueue 生成两个单独的线程,并相应地分配veryHigh 和veryLow 优先级?
编辑:这个问题仍然悬而未决,我正在使用 Travor Harmon 的图像调整大小功能来调整图像大小。有人可以确认它是否是线程安全的吗?
【问题讨论】:
-
我仍然没有找到答案,希望有人能提出一些好主意......这对我的应用程序来说不是一个大问题,但缺乏解决方案让我的大脑感到困扰...... . :)
标签: ios ios5 grand-central-dispatch nsoperationqueue