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
280
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
250
Multithreaded Drawing by Eric Lanz
trianglecocoa
3
210
Unburdened ViewControllers by Jay Thrash
trianglecocoa
9
11k
Automated Acceptance Testing by Josh Johnson
trianglecocoa
4
180
Understanding UIResponder by Dirk Smith
trianglecocoa
5
310
Other Decks in Programming
See All in Programming
機能追加とリーダー業務の類似性
rinchoku
2
890
ECS初心者の仲間 – TUIツール「e1s」の紹介
keidarcy
0
150
モバイルアプリからWebへの横展開を加速した話_Claude_Code_実践術.pdf
kazuyasakamoto
0
300
TDD 実践ミニトーク
contour_gara
1
280
AIエージェント開発、DevOps and LLMOps
ymd65536
1
380
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
210
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
330
プロポーザル駆動学習 / Proposal-Driven Learning
mackey0225
2
700
速いWebフレームワークを作る
yusukebe
5
1.6k
開発チーム・開発組織の設計改善スキルの向上
masuda220
PRO
18
9.8k
OSS開発者という働き方
andpad
5
1.7k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
200
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Gamification - CAS2011
davidbonilla
81
5.4k
A Tale of Four Properties
chriscoyier
160
23k
Documentation Writing (for coders)
carmenintech
74
5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
Visualization
eitanlees
148
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.5k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
800
Optimizing for Happiness
mojombo
379
70k
Building Applications with DynamoDB
mza
96
6.6k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
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