objective-c - 当我发现自己 100% 的时间都在使用非原子时,为什么 "atomic"是 Objective C 中的默认 @property 限定符?

标签 objective-c

在我作为 iOS 开发人员的几年中,我认为我从未在属性上使用过 atomic。如果我可以看到由于线程导致的潜在竞争条件或数据完整性问题,那么在 @property 上使用 atomic 将无济于事。我使用传统的事务/工作单元线程安全技术(使用机制锁、信号量或其他)。

有没有人有(或知道)使用 atomic 的实际示例? (我希望看到一些实际/实用的代码示例)

在写了十亿次nonatomic之后,我还想知道为什么Apple决定将atomic设为默认值。

最佳答案

至于你遇到的第一个问题,可能是因为

Although “atomic” means that access to the property is thread-safe, simply making all the properties in your class atomic does not mean that your class or more generally your object graph is “thread safe”—thread safety cannot be expressed at the level of individual accessor methods.

至于为什么苹果默认将其设为原子,我认为没有什么好的理由,这只是一个糟糕的设计决定。 WWDC session 上的人一再鼓励人们尽可能使用非原子来消除性能影响。

关于objective-c - 当我发现自己 100% 的时间都在使用非原子时,为什么 "atomic"是 Objective C 中的默认 @property 限定符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168331/

相关文章:

ios - UIViewController 内的 UIsearchController 使用自动布局

ios - 根据触摸位置拖动 SKSpriteNode

objective-c - 在 UIWebview 中添加了空格 - 在 iOS7 和 iOS8 中删除了 UIWebView 空格

ios - 导航中实例的不确定性 : UITabBarController

iphone - 当“应用程序不在后台运行”设置为"is"时,无法使用 facebook ios sdk 在 facebook 墙上发帖

ios - 如何从 NSPersistentStoreDidImportUbiquitousChangesNotification 获取插入和更新的对象?

ios - UITableView - 初始加载时未着色的单元格?

ios - Postman 给出成功响应,而通过代码调用相同的 API 时抛出错误

iphone - EXC_BAD_ACCESS 当 popViewController

ios - UIButton 文本缩写?