Today Tips

Atomic, NonAtomic, Synchronous, Asynchronous


Asynchronous never block the main thread waiting for a network response.

Asynchronous can be either synchronous on a separate thread or scheduled in the run loop of any thread.

Synchronous blocks main thread until they complete the request.

Atomic it always returns fully initialized objects, any get/set of a property on one thread must complete before another can access it.

If you imagine the following function occurring on two threads at once you can see the results would not be pretty.

Screen Shot 2017-10-30 at 2.30.06 PM

Pros: Return of fully initialized objects each time makes it best choice in case of multi-threading.Cons: Performance hit, makes execution a little slower- NonAtomic: Unlike Atomic, it doesn't ensure fully initialized object return each time.

Pros: Extremely fast execution.

Declaring a property atomic makes compiler generate additional code that prevents concurrent access to the property. This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. Compared to setting or getting a primitive value or a pointer, locking and unlocking a semaphore is expensive (although it is usually negligible if you consider the overall flow of your app).Since most of your classes under iOS, especially the ones related to UI, will be used in a single-threaded environment, it is safe to drop atomic (i.e. write nonatomic, because properties are atomic by default): even though the operation is relatively inexpensive, you do not want to pay for thing


s that you do not need.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了