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
260
iPhone 5 and You! by Ameir Al-Zoubi
trianglecocoa
0
170
Grand Central Dispatch by Jody Hagins
trianglecocoa
2
460
Instruments: Leaks by Trevor Brown
trianglecocoa
5
130
Foundation Collections by Kevin Conner
trianglecocoa
3
230
Multithreaded Drawing by Eric Lanz
trianglecocoa
3
200
Unburdened ViewControllers by Jay Thrash
trianglecocoa
9
11k
Automated Acceptance Testing by Josh Johnson
trianglecocoa
4
170
Understanding UIResponder by Dirk Smith
trianglecocoa
5
280
Other Decks in Programming
See All in Programming
State of Namespace
tagomoris
5
2.4k
カウシェで Four Keys の改善を試みた理由
ike002jp
1
120
読書シェア会 vol.4 『ダイナミックリチーミング 第2版』
kotaro666
0
110
generative-ai-use-cases(GenU)の推しポイント ~2025年4月版~
hideg
1
370
파급효과: From AI to Android Development
l2hyunwoo
0
160
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
240
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
110
インプロセスQAにおいて大事にしていること / In-process QA Meetup
medley
0
140
プロダクト横断分析に役立つ、事前集計しないサマリーテーブル設計
hanon52_
3
540
Qiita Bash
mercury_dev0517
2
220
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
760
RuboCop: Modularity and AST Insights
koic
2
2.4k
Featured
See All Featured
KATA
mclloyd
29
14k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.5k
Code Review Best Practice
trishagee
67
18k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Writing Fast Ruby
sferik
628
61k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
780
Designing Experiences People Love
moore
142
24k
The Cult of Friendly URLs
andyhume
78
6.3k
The Cost Of JavaScript in 2023
addyosmani
49
7.8k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
105
19k
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