相关疑难解决方法(0)

调度队列:如何判断它们是否正在运行以及如何阻止它们

我正在玩GCD,我写了一个玩具CoinFlipper应用程序.

这是翻转硬币的方法:

- (void)flipCoins:(NSUInteger)nFlips{

    // Create the queues for work
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL);

    // Split the number of flips into whole chunks of kChunkSize and the remainder.
    NSUInteger numberOfWholeChunks = nFlips / kChunkSize;
    NSUInteger numberOfRemainingFlips = nFlips - numberOfWholeChunks * kChunkSize;

    if (numberOfWholeChunks > 0) {
        for (NSUInteger index = 0; index < numberOfWholeChunks; index++) {
            dispatch_async(queue, ^{
                NSUInteger h = 0;
                NSUInteger t = 0;
                flipTheCoins(kChunkSize, &h, &t);
                dispatch_async(mainQueue, ^{
                    self.nHeads += h; …
Run Code Online (Sandbox Code Playgroud)

cocoa multithreading objective-c grand-central-dispatch

33
推荐指数
2
解决办法
4万
查看次数

删除/停止dispatch_async iOS

我创建了一个dispatch_async来将数据下载到Web服务.

我使用possibilida中的按钮创建了一个警报,以取消/阻止下载.

我的问题是:

如何删除/停止dispatch_async?

这是我的发送:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    doStuff();
}
Run Code Online (Sandbox Code Playgroud)

objective-c grand-central-dispatch ios swift

2
推荐指数
1
解决办法
2398
查看次数