0
我有一些核心數據條目,這些條目在應用程序啓動時都被提取並拋入數組中。 當我使用deleteObject:
從Core Data中刪除一個對象時,我通過再次讀取所有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>)"
)
在我的AppDelegate中,有一個名爲' - (void)saveContext'的方法,這是在創建項目時添加的。我猜'managedObjectContext'保存在這裏。除此之外,我什麼也沒做。有另一種方法可以拯救它嗎? – simonbs 2011-04-25 18:25:14
'NSManagedObjectContext'有一個名爲'save:'的方法。假設您所指的是模板中的一個,應用程序委託中的saveContext版本僅在應用程序終止時保存。當然,在iOS4中幾乎從來沒有。 – 2011-04-25 18:29:53
太棒了!我認爲它是使用'saveContext'保存的。非常感謝你!那麼,現在我已經知道了。真棒:-) – simonbs 2011-04-25 18:38:00