核心数据删除了启动时重新出现的对象
我有一些核心数据条目,当应用程序启动时,这些条目都会被获取并放入数组中。 当我使用 deleteObject:
从 Core Data 中删除对象时,我通过再次读取所有 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>)"
)
I have some Core Data entries which are all fetched and thrown into an array when the application starts up.
When I delete an object from Core Data using deleteObject:
I refresh this array by reading all Core Data entries again and throw them into the array. Here, the Core Data object is correctly deleted and is not loaded into the new array. But when I launch the application again, the entries are not removed. They are loaded again.
Does anyone have an idea why my Core Data objects are not correctly removed?
This is where I delete an object:
// 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];
This is how I set the array of Core Data entries on start up
// 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);
Here is some data which I print using NSLog
. This might help solving the issue and will show, that these notes are deleted but reappers.
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>)"
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您保存您的
managedObjectContext
吗?据我了解,添加、修改和删除对象只会真正影响它们在内存中的表示。您还需要保存上下文以保留它。Do you save your
managedObjectContext
? As I understand it, adding, amending and deleting objects only really affects their in-memory representation. You also need to save the context to persist it.