Fre*_*Lee 3 objective-c nsoperationqueue ios nsblockoperation cloudkit
注意:
这只是一个概念验证。
真正的后台任务是不断请求原始数据的“HTTP Get”并通过主线程显示;一经请求。
场景:
1)按需切换后台任务(循环)。
2) 后台任务在每次迭代时通知主线程 UI。
3) 只有一 (1) 个块操作在队列中运行。
Modus Operandus
1) 使用 NSBlockOperation 来包含后台代码。
2)使用区域BOOL来切换循环;通过 IBAction。
问题
1) 编译器将 BOOL 'isRunning' 标记为强链接:
在此块中强烈捕获“自我”可能会导致保留循环。
2)在尝试添加块操作之前,我检查了队列中是否有任何操作。
但我总是收到以下错误:
-[NSOperationQueue addOperation:]: 操作完成,不能入队
除了上述问题之外,这种概念验证似乎正在发挥作用。
问题:
1) 为什么编译将 BOOL 'running' 标记为一个强行,而它只是一个缩放器?
2)如果在队列中没有找到,为什么我不能通过添加另一个 NSBlockOperation 来重用 NSOperationQueue?
以下是整个代码:
#define START 0
#define STOP 1
@interface ricViewController ()
@property (assign) BOOL running;
@end
@implementation ricViewController {
NSOperationQueue *operationQueue;
NSBlockOperation *blockOperation;
void (^backgroundBlock)(void);
}
@synthesize running = isRunning;
#pragma mark - ViewController methods
- (void)viewDidLoad {
operationQueue = [NSOperationQueue new];
[operationQueue setMaxConcurrentOperationCount:1];
[operationQueue setName:@"RicQueue"];
[self buildBackgroundBlock];
blockOperation = [NSBlockOperation blockOperationWithBlock:backgroundBlock];
[super viewDidLoad];
}
// -------------------------------------------------------------------------------------------------------------------
- (void)didReceiveMemoryWarning {
operationQueue = nil;
}
// -------------------------------------------------------------------------------------------------------------------
#pragma mark - Local methods
- (void)buildBackgroundBlock {
static int k = 0;
backgroundBlock = ^{
while (isRunning) { // 1) *** compiler warning flag: strong link warning ***
sleep(1);
if ([NSThread isMainThread]) {
NSLog(@"{backgroundBlock} *** Main Thread *** ***");
} else {
NSString *myString = [NSString stringWithFormat:@"{backgroundBlock} count = %i", k++];
NSLog(myString);
dispatch_async(dispatch_get_main_queue(), ^{
self.dataLabel.text = myString;
});
}
}
};
}
// -------------------------------------------------------------------------------------------------------------------
#pragma - Action methods
- (IBAction)exitAction:(UIButton *)sender {
exit(0);
}
// -------------------------------------------------------------------------------------------------------------------
- (IBAction)segmentedAction:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex) {
case START:
NSLog(@"START");
self.running = YES;
if (operationQueue.operationCount < 1) {
[operationQueue addOperation:blockOperation]; // 2) *** fatal error on 2nd pass.
}
break;
case STOP:
NSLog(@"STOP");
self.running = NO;
break;
}
return;
}
@end
Run Code Online (Sandbox Code Playgroud)
控制台输出:
BackgroundTask[3759:c07] STOP
BackgroundTask[3759:c07] START
BackgroundTask[3759:1303] {backgroundBlock} count = 0
BackgroundTask[3759:1303] {backgroundBlock} count = 1
BackgroundTask[3759:1303] {backgroundBlock} count = 2
BackgroundTask[3759:1303] {backgroundBlock} count = 3
BackgroundTask[3759:1303] {backgroundBlock} count = 4
BackgroundTask[3759:1303] {backgroundBlock} count = 5
BackgroundTask[3759:1303] {backgroundBlock} count = 6
BackgroundTask[3759:1303] {backgroundBlock} count = 7
BackgroundTask[3759:c07] STOP
BackgroundTask[3759:1303] {backgroundBlock} count = 8
Run Code Online (Sandbox Code Playgroud)
我做了进一步的研究,发现
* 我必须为每个 'addOperation' *重新创建NSBlockOperation 对象,因为 NSOperationQueue拒绝重新排队相同的 NSOperation 对象。
因此有以下解决方案:
- (IBAction)segmentedAction:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex) {
case START:
NSLog(@"START");
self.running = YES;
blockOperation = nil;
[self buildBackgroundBlock];
blockOperation = [NSBlockOperation blockOperationWithBlock:backgroundBlock];
[operationQueue addOperation:blockOperation];
break;
case STOP:
NSLog(@"STOP");
self.running = NO;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
...至于编译器将 BOOL 'isRunning' 与静态 'self' 相关联:
在此块中强烈捕获“自我”可能会导致重新训练循环。
所有对“自我”的引用都应该通过一个薄弱的链接。
- (void)buildBackgroundBlock {
static int k = 0;
BOOL myRunning = isRunning;
__weak ricViewController *weakObject = self;
backgroundBlock = ^{
while (myRunning) {
sleep(1);
if ([NSThread isMainThread]) {
NSLog(@"{backgroundBlock} *** Main Thread *** ***");
} else {
NSString *myString = [NSString stringWithFormat:@"{backgroundBlock} count = %i", k++];
NSLog(myString);
dispatch_async(dispatch_get_main_queue(), ^{
weakObject.dataLabel.text = [myString copy];
});
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7858 次 |
最近记录: |