Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Core Data

beny
February 22, 2013

Core Data

Simple introduction presentation about Core Data

beny

February 22, 2013

Transcript

20 slides
  1. + SDK support (Xcode, iCloud, migration, undo/redo) + better performance

    (lazy loading, cache) + independent on storage + validations, relationships, migrations - sometimes not effective (group UPDATE) - migrations - concurrency + SQL + common knowledge - missing SDK support - migrations - concurrency
  2. Managed Object Model • NSManagedObjectModel • entity, relation, fetched properties

    • used by Persisten Store Coordinator • .xcdatamodel → .mom
  3. // Returns the managed object model for the application. //

    If the model doesn't already exist, it is created from the application's model. - (NSManagedObjectModel *)managedObjectModel { if (_managedObjectModel != nil) { return _managedObjectModel ; } NSURL *modelURL = [[ NSBundle mainBundle ] URLForResource :@"BazarTip" withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc ] initWithContentsOfURL :modelURL]; return _managedObjectModel ; }
  4. // Returns the persistent store coordinator for the application. //

    If the coordinator doesn't already exist, it is created and the application's store added to it. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator ; } NSURL *storeURL = [[ self applicationDocumentsDirectory ] URLByAppendingPathComponent :@"BazarTip.sqlite" ]; NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc ] initWithManagedObjectModel :[self managedObjectModel ]]; if (![_persistentStoreCoordinator addPersistentStoreWithType : NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolved error %@, %@" , error, [error userInfo]); abort(); } return _persistentStoreCoordinator ; }
  5. Managed Object Context • NSManagedObjectContext • the top of Core

    Data stack • isn't thread-safe • main vs private
  6. // Returns the managed object context for the application. //

    If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. - (NSManagedObjectContext *)managedObjectContext { if (_managedObjectContext != nil) { return _managedObjectContext ; } NSPersistentStoreCoordinator *coordinator = [ self persistentStoreCoordinator ]; if (coordinator != nil) { _managedObjectContext = [[NSManagedObjectContext alloc ] init]; [_managedObjectContext setPersistentStoreCoordinator :coordinator]; } return _managedObjectContext ; }
  7. Managed Object • NSManagedObject • model • usually subclassed •

    @dynamic • NSManagedObjectID • !!! DON'T use description method
  8. CRUD Keyword *keyword = [[ Keyword alloc] initWithEntity :@"Keyword" insertIntoManagedObjectContext

    :self.context] Create NSString *keywordName = keyword. name; NSArray *ads = keyword. ads; Read ad.title = @"bar"; ad.keyword = keyword; ad.keyword.name = @"foo"; Update [self.context deleteObject:keyword]; Delete [self.context save:&error]; Save context
  9. NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName : NSStringFromClass ([Keyword class])]; request.sortDescriptors

    = @[[NSSortDescriptor sortDescriptorWithKey :@"name" ascending:YES]]; request.predicate = [NSPredicate predicateWithFormat :@"read == %d", NO]; request.fetchLimit = 3; NSError *error = nil; NSArray *results = [self.mainContext executeFetchRequest :request error:&error]; if (error) { NSLog(@"Error during fetching" ) }
  10. Core Data and cocurrency • before iOS 5 with mergeChangesFromContextDidSaveNotification

    • iOS 5+ init from another context with NSManagedObjectContextConcurrencyType and then just save
  11. Advanced topics • concurrency • hard/soft migrations • NSFetchedResultsController •

    different persistent store • faults • UIDocuments, iCloud, Spotlight, Quick Look • fetched properties

More Decks by beny

Other Decks in Programming