dispatch_once_tは一度だけ実行

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i < 100; i++) {
        static dispatch_once_t token;
        dispatch_once(&token, ^{
            NSLog(@"実行");
        });
    }
}

dispathch_once_tもう一度実行したい?

http://stackoverflow.com/questions/10930044/possible-to-reset-state-of-dispatch-once-in-unit-test-to-make-them-run-again

dispatch_once_t is a typedefd long. Its false value is 0

テストのために0にしたい場合に、tearDownで0にする方法はthredsafeではないので危ない

非同期処理

disptach_get_global_queueで非同期スレッドのキューを取得し、dispatch_asyncでそこに処理を追加

イメージ読み込みに例

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // Download the image in the background
    UIImage *image = [self getImageFromURL:URLString];

    dispatch_async(dispatch_get_main_queue(), 0), ^{
        // Back on the main thread, set the image in the cell.
        if (cell.imageView.tag == index) { // only if the cell is on the same index.
            cell.imageView.image = image;
        }
    });
});

参照

results matching ""

    No results matching ""