Implementing next threading solutions in Swift / Objective-C

前端 未结 2 456
执念已碎
执念已碎 2021-01-29 07:44

I started to learn programming for iOS and I want to find out how to implement next Java functions with threads in Swi

2条回答
  •  温柔的废话
    2021-01-29 08:25

    For 2, I would use serial queue:

    dispatch_queue_t myQueue;
    

    And implement my queue as of below:

    myQueue = dispatch_queue_create("my_serial_queue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(myQueue, ^{
        for (int i = 0; i < 10000; i++) {
            usleep(100);
        }
        NSLog(@"Task 1 done");
    });
    
    dispatch_async(myQueue, ^{
        NSLog(@"Task 2 done");
    });
    

提交回复
热议问题