我正在尝试使用NSOperationon 在后台线程上实现搜索iOS.我不想继承子类,NSOperation所以这就是我正在做的事情:
[searchQueue cancelAllOperations];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self
elector:@selector(filterContentForSearchText:)
object:self.searchDisplayController.searchBar.text];
[searchQueue addOperation:op];
[op release];
Run Code Online (Sandbox Code Playgroud)
搜索方法包括for循环,用于检查正在搜索的内容是否在数组中.现在,当我取消NSOperation通过调用时cancelAllOperations,for循环继续在数组中运行.我想阻止这一点,并想知道在for循环中调用它是否合法:
if ([[[searchQueue operations] objectAtIndex:0] isCancelled]) {
[tmp_array release]; // tmp_array is used to hold temporary results
[pool drain]; // pool is my autorelease pool
return;
}
Run Code Online (Sandbox Code Playgroud)