我如何使用块来改变iOS中的执行上下文?

时间:2015-06-26 16:48:48

标签: ios objective-c full-text-search objective-c-blocks

我使用搜索栏使用以下代码搜索文本,但在执行过程中它会卡住我的UI,所以如何使用块执行该代码任何建议?

我的代码:

 for(NSMutableDictionary *dicSearch in arrSearchResult)
    {
        NSString *strParagraph = [self stringByStrippingHTML:[dicSearch objectForKey:@"Topic_content"]];

        NSString *strChapter = [dicSearch objectForKey:@"Chapter_name"];
        NSString *strChapterId = [dicSearch objectForKey:@"Chapter_id"];
        NSString *strTopicId = [dicSearch objectForKey:@"Topic_Id"];

        NSArray *arr = [strParagraph componentsSeparatedByString:@"\n"];

        for(NSString *strContent in arr)
        {
            if ([[strContent uppercaseString] rangeOfString:[objSearchBar.text uppercaseString]].location != NSNotFound)
            {
                NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
                [dic setObject:strContent forKey:@"Content"];
                [dic setObject:strChapter forKey:@"Chapter_name"];
                [dic setObject:strChapterId forKey:@"Chapter_id"];
                [dic setObject:strTopicId forKey:@"Topic_Id"];

                [arrSearchedChapter addObject:dic];
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //put your code in here
});