UPDATE 22.10.2014
Thanks to @Rob for great comments and suggestions. I did read up on how to best deal with my problem and I particularly liked the Mike Ash blog on the subject, however I am still struggling to get this to work with any speedup. The following works.
-(void) addDictionaryItem:(NSString *) mykey withURL:(NSString *)myurl isDir:(BOOL) myIsDir andLR:(NSString *)LR
{
NSDictionary __block *mydict=nil;
dispatch_barrier_async(queue_sync, ^{
mydict = [self.dict objectForKey:mykey];
if ( mydict !=nil) {
NSMutableArray *myarray = [mydict objectForKey:@"myarray"];
NSMutableArray *myarrayIsDir = [mydict objectForKey:@"isdir"];
NSMutableArray *myarrayLR = [mydict objectForKey:@"LR"];
if (![myarray containsObject:myurl]){
[myarray addObject:myurl];
[myarrayLR addObject:LR];
[myarrayIsDir addObject:[NSNumber numberWithBool:myIsDir]];
}
}
else {
NSMutableArray *arrayOfFiles = [NSMutableArray array];
[arrayOfFiles addObject:myurl];
NSMutableArray *arrayIsDir = [NSMutableArray array];
[arrayIsDir addObject:[NSNumber numberWithBool:myIsDir]];
NSMutableArray *arrayOfLR = [NSMutableArray array];
[arrayOfLR addObject:LR];
NSMutableDictionary *attrDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:arrayOfLR, @"LR",
arrayOfFiles, @"myarray", arrayIsDir, @"isdir",
nil];
[self.dict setObject:attrDict forKey:mykey];
}
});
}
Basically I am just wrapping the whole method in a dispatch_barrier_async call. The queue is defined as queue_sync = dispatch_queue_create("com.my.queue", DISPATCH_QUEUE_CONCURRENT);. The new method finishes at the same speed as my previous approach. I tried to implement a separate concurrent read of the dictionary mydict = [self.dict objectForKey:mykey]; but this did not work as the dictionary may be updated while it is being read. Eventually I will move towards a better approach than having three separate arrays to keep track of. The reason I need this is because I only want to keep track of children of nodes that fulfill certain requirements. This means I have to keep my own recursive record of children and children's children in a tree structure. Currently this works but its slow.
Suggestions as to ways to make the current method be even more efficient are greatly welcomed.