我知道在后台执行某事的两种方法。
1:
[self performSelectorInBackground:<#(SEL)#> withObject:<#(id)#>]
2:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
...
});
两种方法都帮不了我。我向game Center报告玩家的游戏分数,当我这样做时,在iPod 4G等旧设备上的游戏玩法明显滞后。但其实并不着急。是否有可能使此代码在低CPU利用率下执行?我在游戏结束后这样做,但用户可以立即重新开始游戏,他会看到大约2秒的延迟。
评分报告代码:
- (void) reportScore:(int64_t)score forLeaderboardID:(NSString*)category newBestTime:(BOOL)newBestTime {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
scoreReporter.context = 0;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
//nothing here
}];
}
使用低优先级队列:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
...
});
只是另一个建议,我不确定它是否有效:
-(void)lowPriorityThread {
NSBlockOperation *b = [NSBlockOperation blockOperationWithBlock:^{
//your code
}];
b.queuePriority = NSOperationQueuePriorityVeryLow;
[[NSOperationQueue currentQueue] addOperation:b];
}