开发者

nonatomic property in model class when using NSOperationQueue (iPhone)?

开发者 https://www.devze.com 2023-01-02 03:28 出处:网络
I have a custom model class with an NSMutableData ivar that will be accessed by custom NSOperation subclasses (using 开发者_StackOverflow社区an NSOperationQueue).I think I can guarantee thread-safe ac

I have a custom model class with an NSMutableData ivar that will be accessed by custom NSOperation subclasses (using 开发者_StackOverflow社区an NSOperationQueue). I think I can guarantee thread-safe access to the ivar from multiple NSOperations by using dependencies, and I can guarantee that I don't access the ivar from other code (say my main app thread) by waiting until the Q has finished all operations.

Should I use a nonatomic property specification, or leave it atomic? Is there a significant impact on performance?


Andrew, whether it's significant depends on what you are doing. If your Operations are uploading movies to youtube and each operation needs to read the data once then it doesn't make the slightest bit of difference - just leave it as atomic.

Otherwise you need to profile to see if it is significant. If you are sure (you don't sound that sure) that the NSMutableData will never be accessed from two or more threads simultaneously (however you do it, lock, barriers, or just waiting) then you don't have a need for it to be atomic.


Premature optimisation is the root of all evil.

Leave it atomic until you find out for sure that there is a performance issue.


If it's a mutable object then your biggest enemy is concurrent mutation, not inconsistent property access.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号