我的表视图控制器包含单元格集合,每个单元格显示2个值:
-------------------------
|小区1 | val1 | val2 |
-------------------------
|小区2 | val1 | val2 |
-------------------------
...
我需要使用这样的算法对这些单元执行串行同步操作:
- 设置核心数据&更新tableView UI
- 迭代细胞
2.1更新tableViewCell UI
2.2迭代值
2.2.1更新值UI
2.2.2下载新值
2.2.2.1如果重复迭代失败
2.2.3保存&更新值UI
2.3更新tableViewCell UI- save&更新tableView UI
醇>
我正在使用NSOperationQueue和NSBlockOperations:
- (void)startOperation:(id)sender {
operationQueue = [NSOperationQueue new];
operationQueue.name = @"Operation Queue";
operationQueue.maxConcurrentOperationCount = 1;
[operationQueue waitUntilAllOperationsAreFinished];
NSBlockOperation * prepareCoreData = [[NSBlockOperation alloc] init];
[prepareCoreData addExecutionBlock:^{
NSLog(@"Prepare Core Data");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Update tableView UI");
}];
}];
[operationQueue addOperation:prepareCoreData];
NSBlockOperation * iterateCells = [[NSBlockOperation alloc] init];
__weak NSBlockOperation * weakIterateCells = iterateCells;
[iterateCells addExecutionBlock:^{
for (Item * item in self.items.fetchedObjects) {
if ([weakIterateCells isCancelled]) break;
NSBlockOperation * setCellUI = [[NSBlockOperation alloc] init];
[setCellUI addExecutionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@" Set tableViewCell UI");
}];
}];
[operationQueue addOperation:setCellUI];
NSBlockOperation * iterateValues = [[NSBlockOperation alloc] init];
__weak NSBlockOperation * weakIterateValues = iterateValues;
[iterateValues addExecutionBlock:^{
for (SearchEngine * s in self.searchEngines) {
if ([weakIterateValues isCancelled]) break;
NSBlockOperation * setValueUI = [[NSBlockOperation alloc] init];
[setValueUI addExecutionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@" Set Value UI");
}];
}];
[operationQueue addOperation:setValueUI];
NSBlockOperation * getValue = [[NSBlockOperation alloc] init];
[getValue addExecutionBlock:^{
NSLog(@" Download Value");
}];
[getValue addDependency:setCellUI];
[operationQueue addOperation:getValue];
NSBlockOperation * updateValue = [[NSBlockOperation alloc] init];
[updateValue addExecutionBlock:^{
NSLog(@" Save Data");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@" Update Value UI");
}];
}];
[updateValue addDependency:getValue];
[operationQueue addOperation:updateValue];
}
}];
[iterateValues addDependency:setCellUI];
[operationQueue addOperation:iterateValues];
NSBlockOperation * updateCellUI = [[NSBlockOperation alloc] init];
[updateCellUI addExecutionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@" Update tableViewCell UI");
}];
}];
[updateCellUI addDependency:iterateValues];
[operationQueue addOperation:updateCellUI];
};
}];
[iterateCells addDependency:prepareCoreData];
[operationQueue addOperation:iterateCells];
NSBlockOperation * completeOperation = [[NSBlockOperation alloc] init];
[completeOperation addExecutionBlock:^{
NSLog(@"Save Data");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Update tableView UI");
}];
}];
[completeOperation addDependency:iterateCells];
[operationQueue addOperation:completeOperation];
}
使用此代码,最后一次操作(#3)在所有迭代完成之前执行。
我正在寻找有关如何正确嵌套操作和队列以维持序列顺序的建议。