简体   繁体   中英

Kill or interrupt dispatch_queue_t method

Using this code I can execute a my code in background.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
        dispatch_async(queue, ^{
            // Perform async operation
            // Call your method/function here
            // Example:
            // NSString *result = [anObject calculateSomething];
            dispatch_sync(dispatch_get_main_queue(), ^{
                // Update UI
                // Example:
                // self.myLabel.text = result;
            });
        });

But I can't find a solution to interrupt this background thread. Is there any method to kill or interrupt a queue?

You should really use NSOperationQueue to manage your code flow and create your NSOperation instances to respect the cancelled property. Once you have done this you can easily suspend the queue (to pause execution of future operations) and cancel any (or all) operations.

Note that it is your responsibility to write your operation to be cancellable - it needs to decide at which points during its processing it's sensible to check the status of cancelled and abort further processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM