主线程,异步线程切换

//TODO:切换到异步线程

  • (void) operationOnAnshyThread:(dispatch_block_t)actionBlock{
    if ([NSThread currentThread].isMainThread) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    if (actionBlock) {
    actionBlock();
    }
    });
    return;
    }
    if (actionBlock) {
    actionBlock();
    }
    }
    //TODO:切回主线程操作
  • (void) operationOnMainThread:(dispatch_block_t)actionBlock{
    if ([NSThread currentThread].isMainThread) {
    if (actionBlock) {
    actionBlock();
    }
    return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
    if (actionBlock) {
    actionBlock();
    }
    });
    }

你可能感兴趣的:(主线程,异步线程切换)