I am creating queue where at first I compress images in background and add it to dictionary in main queue. The next then all images comprised and added I print message. I do not archive it with my code. Why?
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *images = [_adv[@"images"] mutableCopy];
for (NSMutableDictionary *i in images)
{
UIImage *large = [UIImage compressImage:i[@"original"]];
dispatch_async(dispatch_get_main_queue(), ^{
i[@"large"] = large;
[i removeObjectForKey:@"original"];
});
}
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@", _adv);
NSLog(@"\n ГОТОВО");
});
});
Try to reserve the main queue for user interactive work.
You should create your own queue for interacting with your mutable data structures. I suggest using a concurrent queue for this purpose. Writing should be done with a barrier.
You should probably do your background processing on a background queue, and you should probably be doing those things async as you likely don't need the results after kicking off the job and don't want to block the calling thread on the background work.
I would suggest doing something more like this (written directly here, so there may be minor bugs):
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *images = _adv[@"images"];
for (NSMutableDictionary *i in images.copy)
{
UIImage *large = [UIImage compressImage:i[@"original"]];
dispatch_barrier_sync(queueThatYouHaveToGuardAccessToTheseData, ^{
i[@"large"] = large;
[i removeObjectForKey:@"original"];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@", _adv);
NSLog(@"\n ГОТОВО");
});
});
Note that you'll need to create queueThatYouHaveToGuardAccessToTheseData
as a concurrent queue ahead of time. It would probably be better to have queueThatYouHaveToGuardAccessToTheseData be an input to this function or have a single mutex per image (or collection or something like that) so as to avoid having a single global write lock.