我有带有单元格集合的表视图控制器,每个单元格显示 2 个值:
––––––––––––––––––––––––<br> | 单元格1 | val1 | val2 |
––––––––––––––––––––––––<br> | 单元格2 | val1 | val2 |
––––––––––––––––––––––––<br> ...</p>
我需要使用以下算法对这些单元执行串行同步操作:
- 设置核心数据并更新 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- 保存和更新 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) 在所有迭代完成之前执行。
我正在寻找有关如何正确嵌套操作和队列以维护串行顺序的建议。