在Objective-C中,何时为属性声明原子性?

时间:2023-01-15 14:30:46

In Objective-C, under what scenarios/use case do you declare atomicity for your properties in your iOS/cocoa development? Please list practical use cases.

在Objective-C中,在什么情况/用例中你是否在iOS / cocoa开发中声明了属性的原子性?请列出实际用例。

Note: I understand the difference between atomic and non-atomic, but there have been few who have answered this in the context of: "I have used atomic property when I am doing this and it's absolutely needed". Most answers on atomic/non-atomic have been theoretical and superficial.

注意:我理解原子和非原子之间的区别,但很少有人在上下文中回答这个问题:“我在做这个时已经使用了原子属性而且绝对需要它”。关于原子/非原子的大多数答案都是理论和表面的。

2 个解决方案

#1


3  

By default @property makes your property atomic. you specify nonatomic when you don't want it. Atomicity is useful when you have two object that might modify the same object at the same moment.

默认情况下,@ property会使您的属性成为原子。如果不需要,请指定非原子。当您有两个可能在同一时刻修改同一对象的对象时,原子性非常有用。

#2


1  

Put in a simple way:

放一个简单的方法:

If there are a risk that two threads can get/set the same property at the same time, then you need to use atomic. The atomic keyword prohibits a property to be changed when another thread is getting it.

如果存在两个线程可以同时获取/设置相同属性的风险,那么您需要使用atomic。原子关键字禁止在另一个线程获取属性时更改属性。

The default value atomic is slower then nonatomic. That's why you most often use nonatomic

默认值atomic比nonatomic慢。这就是你最经常使用非原子的原因

#1


3  

By default @property makes your property atomic. you specify nonatomic when you don't want it. Atomicity is useful when you have two object that might modify the same object at the same moment.

默认情况下,@ property会使您的属性成为原子。如果不需要,请指定非原子。当您有两个可能在同一时刻修改同一对象的对象时,原子性非常有用。

#2


1  

Put in a simple way:

放一个简单的方法:

If there are a risk that two threads can get/set the same property at the same time, then you need to use atomic. The atomic keyword prohibits a property to be changed when another thread is getting it.

如果存在两个线程可以同时获取/设置相同属性的风险,那么您需要使用atomic。原子关键字禁止在另一个线程获取属性时更改属性。

The default value atomic is slower then nonatomic. That's why you most often use nonatomic

默认值atomic比nonatomic慢。这就是你最经常使用非原子的原因