Worker Thread iOS

前端 未结 4 925
野性不改
野性不改 2021-01-03 10:20

I want to create a background thread on the iPhone that executes some code every 10msec. But before I get lost in the concurrency programming guide and the threading program

4条回答
  •  猫巷女王i
    2021-01-03 10:59

    You could easily use GCD (grand central dispatch) for that. First create a selector that will be called in the background. From here call whatever method you want.

    - (void)backgroundSelector
    {
        // do whatever you want to do
        [self performSelector:@selector(backgroundSelector) withObject:nil afterDelay:0.01];
    }
    

    After that just fire this method for the first time like this

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [self backgroundSelector];
    });
    

    Let me know if that works for you.

提交回复
热议问题