Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying mutable object in completion handler

I have a question about thread safety of the following code example from Apple (from GameKit programming guide)

This is to load achievements from game center and save it locally:

Step 1) Add a mutable dictionary property to your class that report achievements. This dictionary stores the collection of achievement objects.

@property(nonatomic, retain) NSMutableDictionary *achievementsDictionary;

Step 2) Initialize the achievements dictionary.

achievementsDictionary = [[NSMutableDictionary alloc] init];

Step 3) Modify your code that loads loads achievement data to add the achievement objects to the dictionary.

{
    [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error)
        {
            if (error == nil)
            {
                for (GKAchievement* achievement in achievements)
                    [achievementsDictionary setObject: achievement forKey: achievement.identifier];
            }
        }];

My question is as follows - achievementsDictionary object is being modified in the completion handler, without any locks of sort. Is this allowed because completion handlers are a block of work that will be guaranteed by iOS to be executed as unit on the main thread? And never run into thread safety issues?

In another Apple sample code (GKTapper), this part is handled differently:

@property (retain) NSMutableDictionary* earnedAchievementCache; // note this is atomic

Then in the handler:

[GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
        {
            if(error == NULL)
            {
                NSMutableDictionary* tempCache= [NSMutableDictionary dictionaryWithCapacity: [scores count]];
                for (GKAchievement* score in scores)
                {
                    [tempCache setObject: score forKey: score.identifier];
                }
                self.earnedAchievementCache= tempCache;
            }
        }];

So why the different style, and is one way more correct than the other?

like image 837
Sean S Lee Avatar asked Aug 16 '11 22:08

Sean S Lee


1 Answers

Is this allowed because completion handlers are a block of work that will be guaranteed by iOS to be executed as unit on the main thread? And never run into thread safety issues?

This is definitely not the case here. The documentation for -loadAchievementsWithCompletionHandler: explicitly warns that the completion handler might be called on a thread other than the one you started the load from.

Apple's "Threading Programming Guide" classifies NSMutableDictionary among thread-unsafe classes, but qualifies this with, "In most cases, you can use these classes from any thread as long as you use them from only one thread at a time."

So, if both apps are designed such that nothing will be working with the achievement cache till the worker task has finished updating it, then no synchronization would be necessary. This is the only way in which I can see the first example as being safe, and it's a tenuous safety.

The latter example looks like it's relying on the atomic property support to make the switcheroo between the old cache and the new cache. This should be safe, provided all access to the property is via its accessors rather than direct ivar access. This is because the accessors are synchronized with respect to each other, so you do not risk seeing a half-set value. Also, the getter retains and autoreleases the returned value, so that code with the old version will be able to finish working with it without crashing because it was released in the middle of its work. A nonatomic getter simply returns the object directly, which means that it could be deallocated out from under your code if a new value were set for that property by another thread. Direct ivar access can run into the same problem.

I would say the latter example is both correct and elegant, though perhaps a bit over-subtle without a comment explaining how crucial the atomicity of the property is.

like image 70
Jeremy W. Sherman Avatar answered Nov 15 '22 00:11

Jeremy W. Sherman