У меня есть некоторые записи Core Data, которые все выбираются и выбрасываются в массив при запуске приложения.
Когда я удаляю объект из Базовых данных, используя deleteObject:
, я обновляю этот массив, снова считывая все записи Базовых данных и выбрасывая их в массив. Здесь объект Core Data правильно удален и не загружается в новый массив. Но когда я снова запускаю приложение, записи не удаляются. Они снова загружены.
У кого-нибудь есть идея, почему мои объекты базовых данных не удаляются правильно?
Здесь я удаляю объект:
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes" inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *allNotes = [[NSMutableArray alloc] initWithArray:[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]];
for(int i = 0; i < [allNotes count]; i++) {
if([[allNotes objectAtIndex:i] identifier] == [[note objectAtIndex:0] identifier]) {
[managedObjectContext deleteObject:[allNotes objectAtIndex:i]];
NSLog(@"DELETING..");
NSLog(@"%@", [allNotes objectAtIndex:i]);
}
}
[request release];
Так я устанавливаю массив записей базовых данных при запуске
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notes" inManagedObjectContext:managedObjectContext_];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Fetch the records and handle an error
NSError *error;
globalNotes = [[NSMutableArray alloc] initWithArray:[[managedObjectContext_ executeFetchRequest:request error:&error] mutableCopy]];
[request release];
NSLog(@"NOTES ON STARTUP");
NSLog(@"%@", globalNotes);
Вот некоторые данные, которые я печатаю, используя NSLog
. Это может помочь решить проблему и покажет, что эти заметки удалены, но повторно применяются.
2011-04-25 20:11:40.877 Caltio[4039:707] NOTES ON STARTUP
2011-04-25 20:11:40.888 Caltio[4039:707] (
"<Notes: 0x1855d0> (entity: Notes; id: 0x184cd0 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p2> ; data: <fault>)",
"<Notes: 0x1858b0> (entity: Notes; id: 0x184ce0 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p3> ; data: <fault>)"
)
2011-04-25 20:16:41.561 Caltio[4039:707] DELETING..
2011-04-25 20:16:41.569 Caltio[4039:707] <Notes: 0x1855d0> (entity: Notes; id: 0x184cd0 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p2> ; data: {
added = "2011-04-25 17:56:15 +0000";
identifier = 2567446185;
note = Test;
updated = 0;
})
2011-04-25 20:16:41.589 Caltio[4039:707] NOTES AFTER REFRESH:
2011-04-25 20:16:41.595 Caltio[4039:707] (
"<Notes: 0x1858b0> (entity: Notes; id: 0x184ce0 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p3> ; data: {\n added = \"2011-04-25 17:57:26 +0000\";\n identifier = 2567446127;\n note = Test;\n updated = 0;\n})"
)
2011-04-25 20:17:05.103 Caltio[4052:707] NOTES ON STARTUP
2011-04-25 20:17:05.115 Caltio[4052:707] (
"<Notes: 0x1bee60> (entity: Notes; id: 0x1be570 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p2> ; data: <fault>)",
"<Notes: 0x1bf150> (entity: Notes; id: 0x1bdcd0 <x-coredata://DC8B3294-6D87-4236-9E5C-29E11F7330FA/Notes/p3> ; data: <fault>)"
)