执行优先级非常低的代码

时间:2014-07-29 13:35:53

标签: ios multithreading performance cocoa-touch game-center

我知道在后台执行某些操作的两种方法。

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
    }];
}

2 个答案:

答案 0 :(得分:1)

使用低优先级队列:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  ...
});

答案 1 :(得分:0)

只是另一个建议,我不确定它是否有效:

-(void)lowPriorityThread {
    NSBlockOperation *b = [NSBlockOperation blockOperationWithBlock:^{
        //your code
    }];

    b.queuePriority = NSOperationQueuePriorityVeryLow;

    [[NSOperationQueue currentQueue] addOperation:b];
}
相关问题