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 Brewington
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
240
iPhone 5 and You! by Ameir Al-Zoubi
trianglecocoa
0
150
Grand Central Dispatch by Jody Hagins
trianglecocoa
2
430
Instruments: Leaks by Trevor Brown
trianglecocoa
5
120
Foundation Collections by Kevin Conner
trianglecocoa
3
210
Multithreaded Drawing by Eric Lanz
trianglecocoa
3
180
Unburdened ViewControllers by Jay Thrash
trianglecocoa
9
11k
Automated Acceptance Testing by Josh Johnson
trianglecocoa
4
150
Understanding UIResponder by Dirk Smith
trianglecocoa
5
240
Other Decks in Programming
See All in Programming
PHP8.3の機能を振り返る / Review of PHP 8.3 features
seike460
PRO
1
110
Front-end application development, Symfony-style(s)
dunglas
2
1.9k
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
190
Git Rebase
bkuhlmann
11
1.6k
From Spring Boot 2 to Spring Boot 3 with Java 22 and Jakarta EE
ivargrimstad
0
1.1k
ゆるい個人開発のススメ
kuroppe1819
10
970
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
160
Goのmultiple errorsについて (2024年4月版)
syumai
1
290
雑に思考を整理する技術と効能
konifar
58
28k
Build with AI 2024 Seoul - 제로부터 시작하는 Flutter with Gemini 생활 - 박제창
itsmedreamwalker
0
200
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
830
Featured
See All Featured
Done Done
chrislema
178
15k
4 Signs Your Business is Dying
shpigford
175
21k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Designing the Hi-DPI Web
ddemaree
276
33k
Happy Clients
brianwarren
91
6.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
12
4.6k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
24
2.3k
The Brand Is Dead. Long Live the Brand.
mthomps
48
28k
The Invisible Customer
myddelton
114
12k
What's in a price? How to price your products and services
michaelherold
237
11k
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