NSOperationQueue *operationQueue; // for rendering pages on second thread
[operationQueue waitUntilAllOperationsAreFinished];
一句很简单的代码,可以实现UI的异步操作,知道操作完成,才进行进一步刷新。
NSOperationQueue是一个Operation执行队列,你可以将任何你想要执行的Operation添加到Operation Queue中,以在队列中执行。同时Operation和Operation Queue提供了很多可配置选项。Operation Queue的实现中,创建了一个或多个可管理的线程,为队列中的Operation提供可高度自定的执行环境。
DemoCode如下:
// Don't change font feature settings until all secondary thread operations are done
[operationQueue waitUntilAllOperationsAreFinished];
// Set up our font feature bitfield for given optionName
ASDFeaturesBits optionBitSelected = 0;
if (([optionName rangeOfString:ASD_SMALL_CAPITALS]).length > 0 ) {
optionBitSelected = ASDFeaturesSmallCaps;
}
else if (([optionName rangeOfString:ASD_RARE_LIGATURES]).length > 0 ) {
optionBitSelected = ASDFeaturesLigatures;
}
else if (([optionName rangeOfString:ASD_PROP_NUMBERS]).length > 0 ) {
optionBitSelected = ASDFeaturesPropNumbers;
}
else if (([optionName rangeOfString:ASD_STYLISTIC_VARS]).length > 0 ) {
optionBitSelected = ASDFeaturesStylisticVariants;
}
if (optionBitSelected && ((optionBitSelected & ASDFeaturesFeatureMask) != 0)) {
// Font features are most of the time exclusive so for simplicity we allow one feature at a time
viewOptions &= ~ASDFeaturesFeatureMask;
viewOptions |= optionBitSelected;
}
else {
// Passing @"" as the option is used to turn off all options
if ([optionName length] == 0) {
viewOptions = 0;
} else {
viewOptions ^= optionBitSelected;
}
}
if (pageViews[currCoreTextView].selectedFrame == NSUIntegerMax) {
// No selected frame, apply font features to all doc text
NSRange range = NSMakeRange(0, [document.attributedString length]);
[document setFontFeatures:(viewOptions & CoreTextViewOptionsFeatureMask) range:range];
[self relayoutDocFromPage:pageDisplayed];
}
else {
// Apply font features to selected frame
// (note that this only applies for "text" type frames)
CoreTextViewPageInfo* page = [pagesInfo objectForKey:[NSNumber numberWithInt:pageDisplayed]];
CoreTextViewFrameInfo* frame = [page.framesToDraw objectAtIndex:pageViews[currCoreTextView].selectedFrame];
if (frame.frameType == ASDFrameTypeTextFlow) {
[document setFontFeatures:(viewOptions & CoreTextViewOptionsFeatureMask) range:frame.stringRange];
[self relayoutDocFromPage:pageDisplayed];
}
else if (frame.frameType == ASDFrameTypeText) {
ApplyFontFeaturesToString(frame.value, frame.stringRange, (viewOptions & CoreTextViewOptionsFeatureMask));
[frame refreshTextFrame];
[self refreshPage];
}
}