-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonatomic-vs-atomic.html
138 lines (129 loc) · 8.39 KB
/
nonatomic-vs-atomic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Jinhuan Li" />
<meta name="copyright" content="Jinhuan Li" />
<meta name="keywords" content="iOS, interview, iOS, " />
<title>IOS tips #1: Nonatomic VS Atomic · Likers
</title>
<link href="http://cdn-images.mailchimp.com/embedcode/slim-081711.css" rel="stylesheet" type="text/css">
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="http://likers.github.io/theme/css/style.css" media="screen">
<link rel="stylesheet" type="text/css" href="http://likers.github.io/theme/css/solarizedlight.css" media="screen">
<link rel="shortcut icon" href="http://likers.github.io/theme/images/favicon.ico" type="image/x-icon" />
<link rel="apple-touch-icon" href="http://likers.github.io/theme/images/apple-touch-icon.png" />
<link rel="apple-touch-icon" sizes="57x57" href="http://likers.github.io/theme/images/apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon" sizes="72x72" href="http://likers.github.io/theme/images/apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon" sizes="114x114" href="http://likers.github.io/theme/images/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="144x144" href="http://likers.github.io/theme/images/apple-touch-icon-144x144.png" />
<link rel="icon" href="http://likers.github.io/theme/images/apple-touch-icon-144x144.png" />
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://likers.github.io/"><span class=site-name>Likers</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="http://likers.github.io">Home</a></li>
<li ><a href="http://likers.github.io/categories.html">Categories</a></li>
<li ><a href="http://likers.github.io/tags.html">Tags</a></li>
<li ><a href="http://likers.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="http://likers.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page_header span10 offset2">
<h1><a href="http://likers.github.io/nonatomic-vs-atomic.html"> IOS tips #1: Nonatomic VS Atomic </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span8 offset2 article-content">
<h1>What's the most asked question for iOS interview?</h1>
<p>I would say "nonatomic VS atomic" should be the top 3, or even the most popular asked question. Yes there are a lot, a lot Q&A's on the Internet, some of them are really good and have detail explanations while others just told you that </p>
<blockquote>
<p>always use nonatomic because it's faster</p>
</blockquote>
<h2>But Why?</h2>
<p>Short answer is: atomic use extra <strong>"lock"</strong> to the property before a thread calls the setter or getter, and after that <strong>"unlok"</strong> it. This lock cost more resources and makes atomic a little slow (lower performance) than nonatomic which has no lock.</p>
<h2>Why lock?</h2>
<p>This lock is used to keep property safe under multithread and prevents concurrent access to the property. Before one thread initializing the object, lock this object and so that other thread can not access it and prevent the risk of confusion.</p>
<h2>How to keep mutithread safe?</h2>
<p><code>atomic</code> means that the synthesized accessors ensure that a value is always fully retrieved by the getter method or fully set via the setter method. In other words, the properties specified as <code>atomic</code> are guaranteed to always return a fully initialized object.</p>
<h2>Then why always use nonatomic?</h2>
<p>Since most of the classes under iOS, especially the ones related to UI, will be used in a single-threaded environment, it is safe to drop atomic. Even though the atomic is not that expensive, you don't need to pay for what you don't need. </p>
<h2>Important: atomic is not 100% safe</h2>
<p>Atomic is only thread-safe with simple use. It is not guaranteed. <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html">Appledoc</a> says the following:</p>
<blockquote>
<p>Consider an XYZ Person object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.</p>
</blockquote>
<p><strong>That means: <code>atomic</code> only guarantees that you will get/set the property without crash, but will not guarantee to get/set the corret value you want under multithread.</strong> </p>
<p>So, as conclusion, don't forget to use atomic when you are dealing with multithread and keep in mind that atomic should not be your only "safe belt".</p>
<aside>
<hr/>
<nav>
<ul class="articles_timeline">
<li class="previous_article">« <a href="http://likers.github.io/hello-world.html" title="Previous: Hello World!">Hello World!</a></li>
<li class="next_article"><a href="http://likers.github.io/assign-retain-strong-weak-and-copy.html" title="Next: IOS tips #2: Assign, Retain, Strong, Weak, and Copy">IOS tips #2: Assign, Retain, Strong, Weak, and Copy</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<h4>Published</h4>
<time pubdate="pubdate" datetime="2016-01-08T00:00:00-06:00">Jan 8, 2016</time>
<h4>Last Updated</h4>
<div class="last_updated">2016-08-02 00:00:00-05:00</div>
<h4>Category</h4>
<a class="category-link" href="/categories.html#iOS-ref">iOS</a>
<h4>Tags</h4>
<ul class="list-of-tags tags-in-article">
<li><a href="/tags.html#interview-ref">interview
<span>3</span>
</a></li>
<li><a href="/tags.html#iOS-ref">iOS
<span>7</span>
</a></li>
</ul>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
</body>
</html>