Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C, cancel a dispatch queue using UI event
    primarykey
    data
    text
    <p>Scenario:</p> <ul> <li>User taps a button asking for some kind of modification on address book.</li> <li>A method is called to start this modification and an alert view is shown.</li> <li><p>In order to show the alert view and keep the UI responsive, I used dispatch_queue:</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_sync(dispatch_get_main_queue(), ^{ // Show the alert view }); }); </code></pre></li> <li><p>Start the process of address book modification using:</p> <pre><code>dispatch_async(modifyingAddressBookQueue, ^{}); </code></pre></li> </ul> <p>Now, I want to provide the user with the ability to cancel the process anytime (of course before saving the address book). So when he taps the cancel button in the alert sheet, I want to access the dispatch block, set some certain BOOL to stop the process and revert the address book.</p> <p>The problem is, you can't do that! you can't access the block and change any variable inside it since all variables are copied only once. Any change of variables inside the block while being executed won't be seen by the block.</p> <p>To sum up: How to stop a going operation using a UI event?</p> <p>Update:</p> <p>The code for the process: </p> <pre><code>- (void) startFixingModification { _fixContacts = YES; __block BOOL cancelled = NO; dispatch_queue_t modifyingAddressBookQueue; modifyingAddressBookQueue = dispatch_queue_create(sModifyingAddressBookQueueIdentifier, NULL); dispatch_async(modifyingAddressBookQueue, ^{ for (NSMutableDictionary *contactDictionary in _contactArray) { if (!cancelled) { break; } i = i + 1; BOOL didFixContact = [self fixNumberInContactDictionary:contactDictionary]; if (!didFixContact) { _fixedNumbers = _fixedNumbers - 1; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_sync(dispatch_get_main_queue(), ^{ [self setAlertViewProgress:i]; }); }); } }); cancelledPtr = &amp;cancelled; } </code></pre> <p>Code for alertview (my own lib) delegate </p> <pre><code>- (void) alertViewProgressCancel:(ASAlertViewProgress *)alertView { // This is a private lib. if (cancelledPtr) { NSLog(@"stopping"); *cancelledPtr = YES; } } </code></pre> <p>In interface, I declare </p> <pre><code>BOOL* cancelledPtr; </code></pre> <p>Update 2:</p> <p>It's getting really frustrating! for the following code </p> <pre><code>for (NSMutableDictionary *contactDictionary in _contactArray) { NSLog(@"%d", _cancelModification); if (_cancelModification) { break; } } </code></pre> <p>if _cancelModification is set to YES, the for loop is broken and that's OK. Once I comment out the NSLog line, the _cancelModification is neglected when it changes to YES! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload