我需要知道什么时候NSOperationQueue从队列中删除一个操作?我有NSOperationQueue哪个列表NSOperation.从哪个位置NSOperationQueue删除队列中的操作?
因为在NSOPerationqueue完成所有操作后我需要通知.为此,我提到了 这个链接
我需要在我的应用程序中下载目录及其内容.所以我决定实现一个NSOperationQueue,我将NSOperation子类化为实现NSURLRequest等......
问题是我一次添加所有操作,我无法弄清楚何时下载一个目录的所有文件以更新UI并启用此特定目录.
现在我必须等待所有目录中的所有文件都被下载才能更新UI.
我已经为NSOperationQueue的operationCount和NSOperation的isFinished实现了键值观察,但我不知道目录何时包含所有文件!
你有什么主意吗 ?
非常感谢
我有以下代码:
func testFunc(completion: (Bool) -> Void) {
let queue = NSOperationQueue()
queue.maxConcurrentOperationCount = 1
for i in 1...3 {
queue.addOperationWithBlock{
Alamofire.request(.GET, "https://httpbin.org/get").responseJSON { response in
switch (response.result){
case .Failure:
print("error")
break;
case .Success:
print("i = \(i)")
}
}
}
//queue.addOperationAfterLast(operation)
}
queue.waitUntilAllOperationsAreFinished()
print("finished")
}
Run Code Online (Sandbox Code Playgroud)
输出是:
finished
i = 3
i = 1
i = 2
Run Code Online (Sandbox Code Playgroud)
但我希望以下内容:
i = 3
i = 1
i = 2
finished
Run Code Online (Sandbox Code Playgroud)
那么,为什么queue.waitUntilAllOperationsAreFinished()不等待?
如何获得NSOperationQueue的完成块,这里我想从所有操作的开始到结束旋转活动指示器.
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation, operation1, operation3,...] waitUntilFinished:NO];
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在使用NSOperationQueue排队并调用多个地理编码位置查找.我想在所有异步运行的查找完成后调用完成方法.
-(void)geocodeAllItems {
NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init];
[geoCodeQueue setName:@"Geocode Queue"];
for (EventItem *item in [[EventItemStore sharedStore] allItems]) {
if (item.eventLocationCLLocation){
NSLog(@"-Location Saved already. Skipping-");
continue;
}
[geoCodeQueue addOperationWithBlock:^{
NSLog(@"-Geocode Item-");
CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[self geocodeItem:item withGeocoder:geocoder];
}];
}
[geoCodeQueue addOperationWithBlock:^{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
NSLog(@"-End Of Queue Reached!-");
}];
}];
}
- (void)geocodeItem:(EventItem *)item withGeocoder:(CLGeocoder *)thisGeocoder{
NSLog(@"-Called Geocode Item-");
[thisGeocoder geocodeAddressString:item.eventLocationGeoQuery completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Error: geocoding failed for item %@: %@", item, error); …Run Code Online (Sandbox Code Playgroud)