Search code examples
objective-cconcurrencysynchronizationdispatch-async

dispatch_async, keeping an outside variable correct


I have a method set up like so:

// -------------------------------------------------------------------------------
- (void)removeEventMs58:(NSManagedObject *)currentObject commit:(BOOL)commit
{
NSLog (@"   ");
NSLog (@"   ");
NSLog (@"PEC_ManagedObjectController remove EventMs58");
    __block NSString * identEvent = [currentObject valueForKey:@"event58Identifier"];

NSLog(@"remove EventMs58 identEvent: %@", identEvent);
    if ( ![identEvent length]) return;

    [self performSelectorOnMainThread:@selector(setupCalendarDefaults:) withObject:nil waitUntilDone:YES];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
    ^{
        NSError *error = nil;
        NSArray *theEvents = [eventStore calendarItemsWithExternalIdentifier:identEvent];
NSLog(@"remove EventMs58 theEvents: %@", theEvents);

        for (EKEvent *anEvent in theEvents)
        {
NSLog(@"remove EventMs58 before saveEvent");
            if (commit) setUpCalendarCreatedOwnChange = YES;
            if ([eventStore removeEvent:anEvent span:EKSpanFutureEvents commit:YES error:&error] )
            {
NSLog(@"remove EventMs58 success saveEvent");
            }
NSLog(@"after saveEvent error: %@",error);
NSLog(@" ");
NSLog(@" ");
        }
    });
}

this method could be called 14 times fairly quickly in a row for example. with 14 different "identEvent" strings. also in between calls to this method, a similar method could have this same start to the code, (__block NSString * identEvent) potentially further mixing the "identEvent" strings, which of course is not a problem in a single thread.

my question is since this dispatch_async is executed at a different and/or later time, (this method returns quickly), how does an app with such a method, (or does an app) keep the identEvent strings straight, so that it does not mix up the identEvent strings inside this different thread that is running with dispatch_async? also how does it insure that "identEvent" is not deallocated along the way? (possibly 14 different times) before dispatch_async thread is ever executed?


Solution

  • how does an app with such a method, (or does an app) keep the identEvent strings straight, so that it does not mix up the identEvent strings inside this different thread that is running with dispatch_async?

    • DISPATCH_QUEUE_PRIORITY_X queues are concurrent queues, and are FIFO in the sense that tasks within a given queue will begin executing using "first in, first out" order.

    also how does it insure that "identEvent" is not deallocated along the way? (possibly 14 different times) before dispatch_async thread is ever executed?

    • When blocks are copied, it first creates strong references to the object variables used within the block. When using in method: If you access variable by reference, a strong reference is made to self; If you access variable by value, a strong reference is made to the variable which can be overridden by using _block.