0

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?

hokkuk
  • 675
  • 5
  • 21
  • Great response from this [answer](http://stackoverflow.com/questions/16283652/understanding-dispatch-async) about dispatch async behavior and how it FIFO works – Shams Ahmed Jul 08 '14 at 23:48
  • I understood this part from that answer: --------"So, if you send 1000 dispatch_async() blocks to DISPATCH_QUEUE_PRIORITY_DEFAULT, those tasks will start executing in the order you sent them into the queue"-------- but my question has to do with that variable that is not inside the dispatch_async, (i dont' think that variable is held, or maybe i am wrong? ) how could the dispatch_async, use that variable first in and first out, when it is not inside the dispatch_async? – hokkuk Jul 09 '14 at 00:07
  • 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. – Shams Ahmed Jul 09 '14 at 00:27
  • so that I understand what you are saying: it doesn't matter that "identEvent" could possibly change, because each time that the dispatch_async is called, it makes a "copy" of itself? and when it copies itself, it brings along the individual references to that outside variable? long story short, you are saying this method would work just fine just the way it is set up? even though it could be called 14 times in a row, it simply will make 14 copies and or references to that variable, so i do not have to worry about it? – hokkuk Jul 09 '14 at 00:36
  • 1
    Correct; and there's no reason for `__block` here. – Rob Napier Jul 09 '14 at 00:37
  • (I just remembered why __block is used, it is if I want to change the variable inside the block, so ignore that part of my question) – hokkuk Jul 09 '14 at 00:37
  • Right; and by "change" here, we me "change the location that `identEvent` points to." Since `identEvent` goes out of scope even before the block executes, that's not useful. – Rob Napier Jul 09 '14 at 00:38

1 Answers1

1

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.
Shams Ahmed
  • 4,498
  • 4
  • 21
  • 27