我遇到了CoreData中的一些數據庫操作問題。有時候,視圖會在流程中間刷新並顯示錯誤的狀態。有時數據甚至丟失。如何將相關對象從CoreData中的一個對象移動到另一個對象
我有以下CoreData對象模型:
課程有很多學生
現在我想合併兩個療程A和B在一起,這樣以後所有課程B的學生應該被分配到課程一併且課程B不再存在。
(A學生在正好一個療程)
目前我正在做這種方式,其工作的大部分時間,但有那樣的問題:
Course courseA = [self someCourse];
Course courseB = [self someOtherCourse];
for (Student *student in courseB.students) {
student.course = courseA;
}
[managedObjectContext deleteObject:courseB];
NSError *error = nil;
[managedObjectContext save:&error];
if(error != nil) {
NSLog(@"Deletion of Course failed.");
}
有時我得到以下錯誤的設備日誌:
Core Data: warning: validation recovery attempt FAILED with Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn’t be completed. (Cocoa error 1550.)" UserInfo=0x1d97f8e0 {NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1550.), Dangling reference to an invalid object.=null, NSValidationErrorObject=<Course: 0x1cad56c0> (entity: Course; id: 0x1ca69380 <x-coredata:///Course/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D3519> ; data: {
endDateTime = "2014-09-18 17:38:17 +0000";
place = "0x1db4c120 <x-coredata:///Place/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D7394>";
room = "0x18d696a0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Room/p1>";
category = "0x18e8f990 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/CDCategory/p1>";
changed = "2014-09-19 08:04:03 +0000";
startDateTime = "2014-09-18 17:30:01 +0000";
syncInfos = "<relationship fault: 0x1cbb5bb0 'syncInfos'>";
students = (
"0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393>",
"0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383>",
"0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461401>",
"0x1a0a6560 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461387>",
"0x1c90e2f0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461346>",
"0x1cd7fc00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461370>",
"0x1a0bdbf0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461374>",
"0x1cb11090 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461395>",
"0x1cf31fc0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461379>",
"0x1cda6230 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461339>",
"(...and 133 more...)"
);
}), NSAffectedObjectsErrorKey=(
"<Student: 0x1cf6c000> (entity: Student; id: 0x1a36fc70 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461431> ; data: <fault>)"
), NSValidationErrorKey=students, NSValidationErrorValue=Relationship 'students' on managed object (0x1cad56c0) <Course: 0x1cad56c0> (entity: Course; id: 0x1ca69380 <x-coredata:///Course/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D3519> ; data: {
endDateTime = "2014-09-18 17:38:17 +0000";
place = "0x1db4c120 <x-coredata:///Place/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D7394>";
room = "0x18d696a0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Room/p1>";
category = "0x18e8f990 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/CDCategory/p1>";
changed = "2014-09-19 08:04:03 +0000";
startDateTime = "2014-09-18 17:30:01 +0000";
syncInfos = "<relationship fault: 0x1cbb5bb0 'syncInfos'>";
students = (
"0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393>",
"0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383>",
"0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461401>",
"0x1a0a6560 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461387>",
"0x1c90e2f0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461346>",
"0x1cd7fc00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461370>",
"0x1a0bdbf0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461374>",
"0x1cb11090 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461395>",
"0x1cf31fc0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461379>",
"0x1cda6230 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461339>",
"(...and 133 more...)"
);
}) with objects {(
<Student: 0x1d9e48d0> (entity: Student; id: 0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393> ; data: <fault>),
<Student: 0x1ddc6920> (entity: Student; id: 0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383> ; data: <fault>),
<Student: 0x1d991750> (entity: Student; id: 0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461455> ; data: <fault>),
<Student: 0x1cf6c000> (entity: Student; id: 0x1a36fc70 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461431> ; data: <fault>)
)}, NSValidationErrorShouldAttemptRecoveryKey=true} and {
"Dangling reference to an invalid object." = "<null>";
NSAffectedObjectsErrorKey = (
"<Student: 0x1cf6c000> (entity: Student; id: 0x1a36fc70 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461431> ; data: <fault>)"
);
NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1550.)";
NSValidationErrorKey = students;
NSValidationErrorObject = "<Course: 0x1cad56c0> (entity: Course; id: 0x1ca69380 <x-coredata:///Course/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D3519> ; data: {\n endDateTime = \"2014-09-18 17:38:17 +0000\";\n place = \"0x1db4c120 <x-coredata:///Place/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D7394>\";\n room = \"0x18d696a0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Room/p1>\";\n category = \"0x18e8f990 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/CDCategory/p1>\";\n changed = \"2014-09-19 08:04:03 +0000\";\n startDateTime = \"2014-09-18 17:30:01 +0000\";\n points = 1436;\n done = 1;\n notes = \"\";\n syncInfos = \"<relationship fault: 0x1cbb5bb0 'syncInfos'>\";\n students = (\n \"0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393>\",\n \"0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383>\",\n \"0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461401>\",\n \"0x1a0a6560 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461387>\",\n \"0x1c90e2f0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461346>\",\n \"0x1cd7fc00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461370>\",\n \"0x1a0bdbf0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461374>\",\n \"0x1cb11090 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461395>\",\n \"0x1cf31fc0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461379>\",\n \"0x1cda6230 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461339>\",\n \"(...and 133 more...)\"\n );\n})";
NSValidationErrorShouldAttemptRecoveryKey = 1;
NSValidationErrorValue = "Relationship 'students' on managed object (0x1cad56c0) <Course: 0x1cad56c0> (entity: Course; id: 0x1ca69380 <x-coredata:///Course/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D3519> ; data: {\n endDateTime = \"2014-09-18 17:38:17 +0000\";\n place = \"0x1db4c120 <x-coredata:///Place/tA8FBABB4-2BBD-4A5D-ADFA-94D6F08F6A6D7394>\";\n room = \"0x18d696a0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Room/p1>\";\n category = \"0x18e8f990 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/CDCategory/p1>\";\n changed = \"2014-09-19 08:04:03 +0000\";\n startDateTime = \"2014-09-18 17:30:01 +0000\";\n points = 1436;\n done = 1;\n notes = \"\";\n syncInfos = \"<relationship fault: 0x1cbb5bb0 'syncInfos'>\";\n students = (\n \"0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393>\",\n \"0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383>\",\n \"0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461401>\",\n \"0x1a0a6560 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461387>\",\n \"0x1c90e2f0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461346>\",\n \"0x1cd7fc00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461370>\",\n \"0x1a0bdbf0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461374>\",\n \"0x1cb11090 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461395>\",\n \"0x1cf31fc0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461379>\",\n \"0x1cda6230 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461339>\",\n \"(...and 133 more...)\"\n );\n}) with objects {(\n <Student: 0x1d9e48d0> (entity: Student; id: 0x18d073c0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461393> ; data: <fault>),\n <Student: 0x1ddc6920> (entity: Student; id: 0x1c911b00 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461383> ; data: <fault>),\n <Student: 0x1d991750> (entity: Student; id: 0x1c915770 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461433> ; data: <fault>),\n <Student: 0x1dbb38f0> (entity: Student; id: 0x1c920ba0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461435> ; data: <fault>),\n <Student: 0x1cf7b780> (entity: Student; id: 0x1c9298f0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461436> ; data: <fault>),\n <Student: 0x1dd3f4f0> (entity: Student; id: 0x1a09a3b0 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461454> ; data: <fault>),\n <Student: 0x1d9e4850> (entity: Student; id: 0x1cbbe040 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461455> ; data: <fault>),\n <Student: 0x1cf6c000> (entity: Student; id: 0x1a36fc70 <x-coredata://16CBD0F6-B399-4A9C-BD51-5EC98BD99EFC/Student/p461431> ; data: <fault>)\n)}";
}.
我不得不迭代他們因爲這個:http://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors – 2014-09-19 09:58:26