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
250
iPhone 5 and You! by Ameir Al-Zoubi
trianglecocoa
0
150
Grand Central Dispatch by Jody Hagins
trianglecocoa
2
450
Instruments: Leaks by Trevor Brown
trianglecocoa
5
130
Foundation Collections by Kevin Conner
trianglecocoa
3
220
Multithreaded Drawing by Eric Lanz
trianglecocoa
3
190
Unburdened ViewControllers by Jay Thrash
trianglecocoa
9
11k
Automated Acceptance Testing by Josh Johnson
trianglecocoa
4
160
Understanding UIResponder by Dirk Smith
trianglecocoa
5
260
Other Decks in Programming
See All in Programming
macOS でできる リアルタイム動画像処理
biacco42
7
2k
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
24
11k
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1k
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.2k
Tuning GraphQL on Rails
pyama86
2
1.1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
440
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
Honoの来た道とこれから
yusukebe
19
3.1k
gopls を改造したら開発生産性が高まった
satorunooshie
8
250
Vue.js学習の振り返り
hiro_xre
2
130
現場で役立つモデリング 超入門
masuda220
PRO
13
3k
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
How STYLIGHT went responsive
nonsquared
95
5.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
4
370
GraphQLとの向き合い方2022年版
quramy
43
13k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
It's Worth the Effort
3n
183
27k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Into the Great Unknown - MozCon
thekraken
31
1.5k
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