Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UIManagedDocument with Core Data by Alondo Brew...
Search
Triangle Cocoa
July 01, 2012
Programming
1
1.2k
UIManagedDocument with Core Data by Alondo Brewington
Alondo discusses using UIManagedDocument with Core Data at CocoaHeads Raleigh in April 2012
Triangle Cocoa
July 01, 2012
Tweet
Share
More Decks by Triangle Cocoa
See All by Triangle Cocoa
App Store Secrets by Lawrence Ingraham
trianglecocoa
1
290
iPhone 5 and You! by Ameir Al-Zoubi
trianglecocoa
0
180
Grand Central Dispatch by Jody Hagins
trianglecocoa
2
480
Instruments: Leaks by Trevor Brown
trianglecocoa
5
150
Foundation Collections by Kevin Conner
trianglecocoa
3
260
Multithreaded Drawing by Eric Lanz
trianglecocoa
3
220
Unburdened ViewControllers by Jay Thrash
trianglecocoa
9
11k
Automated Acceptance Testing by Josh Johnson
trianglecocoa
4
180
Understanding UIResponder by Dirk Smith
trianglecocoa
5
320
Other Decks in Programming
See All in Programming
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
420
ビルドプロセスをデバッグしよう!
yt8492
0
130
iOSでSVG画像を扱う
kishikawakatsumi
0
180
Go言語はstack overflowの夢を見るか?
logica0419
0
660
テーブル定義書の構造化抽出して、生成AIでDWH分析を試してみた / devio2025tokyo
kasacchiful
0
340
AIのバカさ加減に怒る前にやっておくこと
blueeventhorizon
0
120
Dive into Triton Internals
appleparan
0
280
スマホから Youtube Shortsを見られないようにする
lemolatoon
27
34k
ネストしたdata classの面倒な更新にさようなら!Lensを作って理解するArrowのOpticsの世界
shiita0903
1
170
Kotlinで実装するCPU/GPU 「協調的」パフォーマンス管理
matuyuhi
0
110
kiroとCodexで最高のSpec駆動開発を!!数時間で web3ネイティブなミニゲームを作ってみたよ!
mashharuki
0
990
Researchlyの開発で参考にしたデザイン
adsholoko
0
100
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
RailsConf 2023
tenderlove
30
1.3k
For a Future-Friendly Web
brad_frost
180
10k
Building an army of robots
kneath
306
46k
Why Our Code Smells
bkeepers
PRO
340
57k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
How GitHub (no longer) Works
holman
315
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Unsuck your backbone
ammeep
671
58k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Transcript
UIManagedDocument with CoreData Alondo Brewington
[email protected]
@abrewing
UIManagedDocument Subclass of UIDocument Core Data features
Benefits Asynchronous I/O Autosaving Unmodeled Data (Additional Content API) iCloud
integration
Asynchronous I/O Snapshots taken for open/read /save work happens on
a background queue Completion handler block executed on main queue
Data Load // Create UIManagedDocument with URL UIManagedDocument *myAppDatabase =
[[UIManagedDocument alloc] initWithFileURL:myAppDBFileURL]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; myAppDatabase = options; [myAppDatabase saveToURL:myAppDBFileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){ if (!success) { // Handle the error. }else { // Perform functions for created database (e.g. insert default data) } }];
Autosave Autosave by default Proceed with caution Particularly with relationships
Saving - (void)performUpdate { NSManagedObjectContext *context = myAppDatabase.managedObjectContext; NSSet *inserts
= [context insertedObjects]; if ([inserts count]) { NSLog(@"Inserting %d new objects", [inserts count]); NSError * error = nil; if ([context obtainPermanentIDsForObjects:[inserts allObjects] error:&error] == NO) { NSLog(@"[DataManager performUpdate]! %@", error); } } [myAppDatabase updateChangeCount:UIDocumentChangeDone]; }
Saving [myAppDatabase saveToURL: myAppDatabase.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) { if (success)
{ NSLog(@"Success: saved database"); } }];
Migration // If legacy store exists, copy it to the
new location NSURL *legacyPersistentStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"OldDatabase.sqlite"]; NSFileManager* fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:legacyPersistentStoreURL.path]) { NSLog(@"Old db exists"); NSError* thisError = nil; [fileManager replaceItemAtURL:storeURL withItemAtURL:legacyPersistentStoreURL backupItemName:nil options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:nil error:&thisError]; NSLog([thisError localizedDescription]); } return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType
Demo ToDo List App Data Model DataManage class File Package
Resources WWDC 2011: Session 303 - What’s New in Core
Data on iOS WWDC 2011: Storing Documents in iCloud using iOS 5 iTunes: Stanford - iPad and iPhone Application Development
Questions