in iOS seit 3.0 • schema-driven object graph management and persistence framework • SQLite ist eine Option • fest verankert in Xcode-Templates • grafische Datenmodellierung -> Schema 5
a binary momd-File NSURL *modelURL = [mainBundle URLForResource:@"GiftIdeas" withExtension:@"momd"]; // initialized an instance of NSManagedObjectModel with this URL NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
with the model NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; // URL to the location of the sqlite NSURL *documentsDirectory = [self applicationDocumentsDirectory]; NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"GiftIdeas.sqlite"]; // Add a store with the specified URL to the StoreCoordinator NSError *error = nil; if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Failed to add PersistentStore: %@", [error userInfo]); }
= [NSFetchRequest fetchRequestWithEntityName:@"Person"]; // specify how to sort the fetched data fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]; // initialize a NSFetchedResultsController to handle fetched data NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil]; // register as a delegate to react to changes to the fetched data fetchedResultsController.delegate = self;
new instance with the entity name into context KPBPerson *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext]; NSManagedObjectModel NSEntityDescription ...
a child of the current NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; moc.parentContext = masterManagedObjectContext; ... // save if needed NSError *saveError = nil; if (![moc save:&saveError]) { NSLog(@"failed to save context: %@", saveError); } else { // persist to disk if needed by saving the root context // use a block to save the context on its own queue [masterManagedObjectContext performBlock:^{ NSError *masterSaveError = nil; if (![masterManagedObjectContext save:&masterSaveError]) { NSLog(@"failed to save master: %@", masterSaveError); } }]; }
und Beziehungen werden erstellt • Value-Expression für simples Kopieren, Rechnungen, Bedingungen • MOCs werden für die Migration erstellt -> Achtung! Speicher 55