forked from jessesquires/JSQCoreDataKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreDataStack.html
More file actions
428 lines (406 loc) · 22.5 KB
/
CoreDataStack.html
File metadata and controls
428 lines (406 loc) · 22.5 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html lang="en">
<head>
<title>CoreDataStack Class Reference</title>
<link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
<link rel="stylesheet" type="text/css" href="../css/highlight.css" />
<meta charset='utf-8'>
<script src="../js/jquery.min.js" defer></script>
<script src="../js/jazzy.js" defer></script>
</head>
<body>
<a name="//apple_ref/swift/Class/CoreDataStack" class="dashAnchor"></a>
<a title="CoreDataStack Class Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="../index.html">JSQCoreDataKit Docs</a> (100% documented)</p>
<p class="header-right"><a href="https://github.com/jessesquires/JSQCoreDataKit"><img src="../img/gh.png"/>View on GitHub</a></p>
</div>
</header>
<div class="content-wrapper">
<p id="breadcrumbs">
<a href="../index.html">JSQCoreDataKit Reference</a>
<img id="carat" src="../img/carat.png" />
CoreDataStack Class Reference
</p>
</div>
<div class="content-wrapper">
<nav class="sidebar">
<ul class="nav-groups">
<li class="nav-group-name">
<a href="../Guides.html">Guides</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../getting-started.html">Getting Started</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Classes.html">Classes</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Classes/CoreDataStack.html">CoreDataStack</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Global Variables.html">Global Variables</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Global Variables.html#/s:14JSQCoreDataKit19defaultStoreOptionss10DictionaryVySo8NSObjectCyXlGv">defaultStoreOptions</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Enums.html">Enums</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Enums/MigrationError.html">MigrationError</a>
</li>
<li class="nav-group-task">
<a href="../Enums/ModelFileExtension.html">ModelFileExtension</a>
</li>
<li class="nav-group-task">
<a href="../Enums/SaveResult.html">SaveResult</a>
</li>
<li class="nav-group-task">
<a href="../Enums/StackResult.html">StackResult</a>
</li>
<li class="nav-group-task">
<a href="../Enums/StoreType.html">StoreType</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Functions.html">Functions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Functions.html#/s:14JSQCoreDataKit19defaultDirectoryURL10Foundation0F0VyF">defaultDirectoryURL()</a>
</li>
<li class="nav-group-task">
<a href="../Functions.html#/s:14JSQCoreDataKit11saveContextySo015NSManagedObjectE0C_Sb4waityAA10SaveResultOcSg10completiontF">saveContext(_:wait:completion:)</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Protocols/CoreDataEntityProtocol.html">CoreDataEntityProtocol</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Structs.html">Structs</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Structs/CoreDataModel.html">CoreDataModel</a>
</li>
<li class="nav-group-task">
<a href="../Structs/CoreDataStackFactory.html">CoreDataStackFactory</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="../Typealiases.html">Typealiases</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="../Typealiases.html#/s:14JSQCoreDataKit12ChildContexta">ChildContext</a>
</li>
<li class="nav-group-task">
<a href="../Typealiases.html#/s:14JSQCoreDataKit22PersistentStoreOptionsa">PersistentStoreOptions</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="main-content">
<section>
<section class="section">
<h1>CoreDataStack</h1>
<div class="declaration">
<div class="language">
<pre class="highlight"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">CoreDataStack</span><span class="p">:</span> <span class="kt">CustomStringConvertible</span><span class="p">,</span> <span class="kt">Equatable</span></code></pre>
</div>
</div>
<p>An instance of <code>CoreDataStack</code> encapsulates the entire Core Data stack.
It manages the managed object model, the persistent store coordinator, and managed object contexts.</p>
<p>It is composed of a main context and a background context.
These two contexts operate on the main queue and a private background queue, respectively.
Both are connected to the persistent store coordinator and data between them is perpetually kept in sync.</p>
<p>Changes to a child context are propagated to its parent context and eventually the persistent store when saving.</p>
<div class="aside aside-warning">
<p class="aside-title">Warning</p>
<strong>You cannot create a <code>CoreDataStack</code> instance directly. Instead, use a <code><a href="../Structs/CoreDataStackFactory.html">CoreDataStackFactory</a></code> for initialization.</strong>
</div>
</section>
<section class="section task-group-section">
<div class="task-group">
<div class="task-name-container">
<a name="/Properties"></a>
<a name="//apple_ref/swift/Section/Properties" class="dashAnchor"></a>
<a href="#/Properties">
<h3 class="section-name">Properties</h3>
</a>
</div>
<ul>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC5modelAA0dB5ModelVv"></a>
<a name="//apple_ref/swift/Property/model" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC5modelAA0dB5ModelVv">model</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>The model for the stack.</p>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="k">let</span> <span class="nv">model</span><span class="p">:</span> <span class="kt"><a href="../Structs/CoreDataModel.html">CoreDataModel</a></span></code></pre>
</div>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC11mainContextSo015NSManagedObjectG0Cv"></a>
<a name="//apple_ref/swift/Property/mainContext" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC11mainContextSo015NSManagedObjectG0Cv">mainContext</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>The main managed object context for the stack, which operates on the main queue.
This context is a root level context that is connected to the <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC16storeCoordinatorSo017NSPersistentStoreG0Cv">storeCoordinator</a></code>.
It is kept in sync with <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC17backgroundContextSo015NSManagedObjectG0Cv">backgroundContext</a></code>.</p>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="k">let</span> <span class="nv">mainContext</span><span class="p">:</span> <span class="kt">NSManagedObjectContext</span></code></pre>
</div>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC17backgroundContextSo015NSManagedObjectG0Cv"></a>
<a name="//apple_ref/swift/Property/backgroundContext" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC17backgroundContextSo015NSManagedObjectG0Cv">backgroundContext</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>The background managed object context for the stack, which operates on a background queue.
This context is a root level context that is connected to the <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC16storeCoordinatorSo017NSPersistentStoreG0Cv">storeCoordinator</a></code>.
It is kept in sync with <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC11mainContextSo015NSManagedObjectG0Cv">mainContext</a></code>.</p>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="k">let</span> <span class="nv">backgroundContext</span><span class="p">:</span> <span class="kt">NSManagedObjectContext</span></code></pre>
</div>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC16storeCoordinatorSo017NSPersistentStoreG0Cv"></a>
<a name="//apple_ref/swift/Property/storeCoordinator" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC16storeCoordinatorSo017NSPersistentStoreG0Cv">storeCoordinator</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>The persistent store coordinator for the stack. Both contexts are connected to this coordinator.</p>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="k">let</span> <span class="nv">storeCoordinator</span><span class="p">:</span> <span class="kt">NSPersistentStoreCoordinator</span></code></pre>
</div>
</div>
</section>
</div>
</li>
</ul>
</div>
<div class="task-group">
<div class="task-name-container">
<a name="/Child%20contexts"></a>
<a name="//apple_ref/swift/Section/Child contexts" class="dashAnchor"></a>
<a href="#/Child%20contexts">
<h3 class="section-name">Child contexts</h3>
</a>
</div>
<ul>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC12childContextSo015NSManagedObjectG0CSC0hiG15ConcurrencyTypeO011concurrencyK0_SC013NSMergePolicyK0O05mergenK0tF"></a>
<a name="//apple_ref/swift/Method/childContext(concurrencyType:mergePolicyType:)" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC12childContextSo015NSManagedObjectG0CSC0hiG15ConcurrencyTypeO011concurrencyK0_SC013NSMergePolicyK0O05mergenK0tF">childContext(concurrencyType:mergePolicyType:)</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Creates a new child context with the specified <code>concurrencyType</code> and <code>mergePolicyType</code>.</p>
<p>The parent context is either <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC11mainContextSo015NSManagedObjectG0Cv">mainContext</a></code> or <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC17backgroundContextSo015NSManagedObjectG0Cv">backgroundContext</a></code> dependending on the specified <code>concurrencyType</code>:</p>
<ul>
<li><code>.PrivateQueueConcurrencyType</code> will set <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC17backgroundContextSo015NSManagedObjectG0Cv">backgroundContext</a></code> as the parent.</li>
<li><code>.MainQueueConcurrencyType</code> will set <code><a href="../Classes/CoreDataStack.html#/s:14JSQCoreDataKit04CoreB5StackC11mainContextSo015NSManagedObjectG0Cv">mainContext</a></code> as the parent.</li>
</ul>
<p>Saving the returned context will propagate changes through the parent context and then to the persistent store.</p>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">childContext</span><span class="p">(</span><span class="nv">concurrencyType</span><span class="p">:</span> <span class="kt">NSManagedObjectContextConcurrencyType</span> <span class="o">=</span> <span class="o">.</span><span class="n">mainQueueConcurrencyType</span><span class="p">,</span>
<span class="nv">mergePolicyType</span><span class="p">:</span> <span class="kt">NSMergePolicyType</span> <span class="o">=</span> <span class="o">.</span><span class="n">mergeByPropertyObjectTrumpMergePolicyType</span><span class="p">)</span> <span class="o">-></span> <span class="kt"><a href="../Typealiases.html#/s:14JSQCoreDataKit12ChildContexta">ChildContext</a></span></code></pre>
</div>
</div>
<div>
<h4>Parameters</h4>
<table class="graybox">
<tbody>
<tr>
<td>
<code>
<em>concurrencyType</em>
</code>
</td>
<td>
<div>
<p>The concurrency pattern to use. The default is <code>.MainQueueConcurrencyType</code>.</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>mergePolicyType</em>
</code>
</td>
<td>
<div>
<p>The merge policy to use. The default is <code>.MergeByPropertyObjectTrumpMergePolicyType</code>.</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<h4>Return Value</h4>
<p>A new child managed object context.</p>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:14JSQCoreDataKit04CoreB5StackC5resetySo13DispatchQueueC02onH0_yAA0E6ResultOc10completiontF"></a>
<a name="//apple_ref/swift/Method/reset(onQueue:completion:)" class="dashAnchor"></a>
<a class="token" href="#/s:14JSQCoreDataKit04CoreB5StackC5resetySo13DispatchQueueC02onH0_yAA0E6ResultOc10completiontF">reset(onQueue:completion:)</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Resets the managed object contexts in the stack on their respective threads.
Then, if the coordinator is connected to a persistent store, the store will be deleted and recreated on a background thread.
The completion closure is executed on the main thread.</p>
<div class="aside aside-note">
<p class="aside-title">Note</p>
<p>Removing and re-adding the persistent store is performed on a background queue.
For binary and SQLite stores, this will also remove the store from disk.</p>
</div>
</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">reset</span><span class="p">(</span><span class="n">onQueue</span> <span class="nv">queue</span><span class="p">:</span> <span class="kt">DispatchQueue</span> <span class="o">=</span> <span class="o">.</span><span class="nf">global</span><span class="p">(</span><span class="nv">qos</span><span class="p">:</span> <span class="o">.</span><span class="n">userInitiated</span><span class="p">),</span>
<span class="nv">completion</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt"><a href="../Enums/StackResult.html">StackResult</a></span><span class="p">)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span></code></pre>
</div>
</div>
<div>
<h4>Parameters</h4>
<table class="graybox">
<tbody>
<tr>
<td>
<code>
<em>queue</em>
</code>
</td>
<td>
<div>
<p>A background queue on which to reset the stack.
The default is a background queue with a <q>user initiated</q> quality of service class.</p>
</div>
</td>
</tr>
<tr>
<td>
<code>
<em>completion</em>
</code>
</td>
<td>
<div>
<p>The closure to be called once resetting is complete. This is called on the main queue.</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</li>
</ul>
</div>
</section>
</section>
<section id="footer">
<p>© 2017 <a class="link" href="https://twitter.com/jesse_squires" target="_blank" rel="external">Jesse Squires</a>. All rights reserved. (Last updated: 2017-09-25)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.8.3</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
</div>
</body>
</div>
</html>