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
コンテナでLambdaをデプロイするときに知っておきたかったこと
_takahash
0
180
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
630
5年間継続して開発した自作OSSの記録
bebeji_nappa
0
170
Vibe Codingをせずに Clineを使っている
watany
17
6.1k
AHC045_解説
shun_pi
0
480
リアクティブシステムの変遷から理解するalien-signals / Learning alien-signals from the evolution of reactive systems
yamanoku
3
1.2k
これだけは知っておきたいクラス設計の基礎知識 version 2
masuda220
PRO
24
6.1k
Day0 初心者向けワークショップ実践!ソフトウェアテストの第一歩
satohiroyuki
0
830
海外のアプリで見かけたかっこいいTransitionを真似てみる
shogotakasaki
1
160
State of Namespace
tagomoris
4
770
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.4k
Signal-Based Data FetchingWith the New httpResource
manfredsteyer
PRO
0
160
Featured
See All Featured
Docker and Python
trallard
44
3.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
RailsConf 2023
tenderlove
30
1.1k
Raft: Consensus for Rubyists
vanstee
137
6.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Building an army of robots
kneath
304
45k
Product Roadmaps are Hard
iamctodd
PRO
52
11k
4 Signs Your Business is Dying
shpigford
183
22k
Practical Orchestrator
shlominoach
186
10k
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