Reputation: 27211
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?
Upvotes: 0
Views: 655
Reputation: 119031
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.
Upvotes: 3