`
lizaochengwen
  • 浏览: 643960 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Tried to obtain the web lock from a thread other than the main thread or the we

 
阅读更多
Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread.
原因:在子线程中对UI进行了操作。
NSDate *startTime = [NSDate date];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *fetchedData = [self fetchSomethingFromServer];
        NSString *processedData = [self processData:fetchedData];
        NSString *firstResult = [self calculateFirstResult:processedData];
        NSString *secondResult = [self calculateSecondResult:processedData];
        NSString *resultsSummary = [NSString stringWithFormat:@"First: [%@]\nSecond: [%@]",
                                    firstResult, secondResult];
        dispatch_async(dispatch_get_main_queue(), ^{
            [resultTextView setText:resultsSummary];    // *** 将UI操作放到主线程中执行 ***
        });
        NSDate *endTime = [NSDate date];
        NSLog(@"Completed in %f seconds", [endTime timeIntervalSinceDate:startTime]);
    });
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics