Книга: iOS. Приемы программирования

Решение

Решение

Воспользуйтесь методом экземпляра deleteObject:, относящимся к классу NSManagedObjectContext:

— (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self createNewPersonWithFirstName:@"Anthony"
lastName:@"Robbins"
age:51];
[self createNewPersonWithFirstName:@"Richard"
lastName:@"Branson"
age:61];
/* Сначала создаем запрос выборки данных. */
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSError *requestError = nil;
/* Теперь применим запрос выборки данных к контексту. */
NSArray *persons =
[self.managedObjectContext executeFetchRequest: fetchRequest
error:&requestError];
/* Убеждаемся, что получили массив. */
if ([persons count] > 0){
/* Удаляем последний контакт из массива. */
Person *lastPerson = [persons lastObject];
[self.managedObjectContext deleteObject: lastPerson];
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"Successfully deleted the last person in the array.");
} else {
NSLog(@"Failed to delete the last person in the array.");
}
} else {
NSLog(@"Could not find any Person entities in the context.");
}
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

В приведенном примере кода используется метод createNewPersonWithFirstName: lastName: age:, который мы написали в разделе 16.4.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.090. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз