我有一个关于 dispatch_async 的问题。我想确保队列完成执行,因为我需要返回一个仅在块完成时获得的值。
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NO);
dispatch_async(downloadQueue, ^{
NSArray *photos = [[FlickrFetcher photosInPlace:self.listOfPlacesChosenPlaceDictionary maxResults:50] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
self.photoAtThePlace = photos;
});
});
dispatch_release(downloadQueue);
return [self.photoAtThePlace mutableCopy];
当前返回值将为零,因为调用 return 函数时 dispatch_async(dispatch_get_main_queue(), ^{..} 尚未执行。我如何确保在调用 return 之前完成?谢谢!