8

Core Data 允许我保存,但是当我尝试删除对象时,我收到以下错误:

*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xa6b7e00 <x-coredata://8A687ECB-03F8-47C0-8470-087B9CF032B1-2801-00000A8C9A09CDB7/Notification/p2774005D-4B49-4378-A109-949F15D37032>''

[self.managedObjectContext executeFetchRequest:allFetchRequest onSuccess:^(NSArray *results) {
                NSLog(@"Fetch for Objects with origial_id");
                if (results.count>0)
                {
                    NSLog(@"original_id =  %@", dlPlanDate.original_id);
                    NSLog(@"Results Recieved %i", results.count);
                    for (PlanDate *pd in results)
                    {
                        PlanDateResolved *pdResolved = [NSEntityDescription insertNewObjectForEntityForName:@"PlanDateResolved" inManagedObjectContext:self.managedObjectContext];
                        NSArray *keys = [[[pd entity] attributesByName] allKeys];
                        NSDictionary *dict= [pd dictionaryWithValuesForKeys:keys];
                        [pdResolved setValuesForKeysWithDictionary:dict];
                        [pdResolved setValue:[pd plandate_id] forKey:[pdResolved primaryKeyField]];
                        pdResolved.senderUser = pd.senderUser;
                        pdResolved.objectCreator = pd.objectCreator;
                        pdResolved.users = pd.users;
                        NSLog(@"pd.planDateID %@", pd.plandate_id);
                        [self.managedObjectContext saveOnSuccess:^{

                        NSLog(@"Saving Each Object in the Array  to PlanDateResolved with Original ID");
                            [self.managedObjectContext deleteObject:pd];
                            [self.managedObjectContext saveOnSuccess:^{
                                NSLog(@"Deleted the pd Object object!");
                            } onFailure:^(NSError *error) {
                                NSLog(@"There was an error Deleting pd Object %@", error);
                            }];
                        } onFailure:^(NSError *error) {
                        }];
                    }
                }
            } onFailure:^(NSError *error) {
                NSLog(@"Error fetching: %@", error);
            }];
        }

日志:

2013-10-11 11:53:11.175 ST[2801:c07] Fetch for Objects with origial_id
2013-10-11 11:53:11.175 ST[2801:c07] original_id =  CFADD5A7-C9E8-48B3-91B2-56FDBC0F9BAB
2013-10-11 11:53:11.175 ST[2801:c07] Results Recieved 3
2013-10-11 11:53:11.180 ST[2801:c07] pd.planDateID CFADD5A7-C9E8-48B3-91B2-56FDBC0F9BAB
2013-10-11 11:53:11.182 ST[2801:c07] pd.planDateID 5917071A-8CFD-4C7C-A60F-A76FA4814429
2013-10-11 11:53:11.183 ST[2801:c07] pd.planDateID 1998BB90-D6FC-4226-977C-3414C4A57078
2013-10-11 11:53:11.913 ST[2801:c07] You updated the Accepted object!
2013-10-11 11:53:11.913 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:11.913 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:12.155 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:35.482 ST[2801:c07] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xa6b7e00 <x-coredata://8A687ECB-03F8-47C0-8470-087B9CF032B1-2801-00000A8C9A09CDB7/Notification/p2774005D-4B49-4378-A109-949F15D37032>''
*** First throw call stack:
(0x24e7012 0x1cfae7e 0x1a20a48 0x1a515e8 0x1ac2879 0x1a59627 0x1a59271 0x1ace025 0x1a53cc1 0x1e7f014 0x1e6ed5f 0x1e6eaa3 0x1a53c8b 0x1a53aee 0x1a30eac 0x1ab0c1 0x1a5398f 0x1e7f014 0x1e6f7d5 0x248daf5 0x248cf44 0x248ce1b 0x24027e3 0x2402668 0x92effc 0x418d 0x25c5)
libc++abi.dylib: terminate called throwing an exception
4

4 回答 4

13

当托管对象不可访问时,这意味着 Core Data 无法在持久存储文件中找到该对象。发生这种情况有几个原因。

我不知道从哪里来executeFetchRequest:onSuccess:saveOnSuccess:但假设他们按照他们的方法名称所暗示的那样去做(并且没有可怕的错误),上面的代码中没有明显的原因为什么会发生这种情况。所以它是作为你在其他地方工作的副作用而发生的。

最有可能的是:您还在其他地方删除了这些对象,并且此删除发生在您执行 fetch 之后但代码崩溃之前的某个时间。结果,当上面的代码试图删除它们时,它们已经消失了。您正在使用的神秘 Core Data 方法似乎是异步运行的(基于您在到达崩溃点之前多次打印的事实pd.planDateID,因此这可能在同一个线程上或在不同的线程上。

[所有这些都假设您的日志语句实际上来自您发布的代码。这有点令人怀疑,因为日志中包含一条消息:“您更新了已接受的对象!” ,代码中没有出现。]

于 2013-10-11T23:31:41.333 回答
10

我同意 Tom Harrington 关于根本问题的解释。此外,很高兴知道自从iOS 9以来这种行为已经改变。在 iOS 9 上没有抛出NSObjectInaccessibleException但会出现 CoreData 警告,如下所示:

CoreData: warning: An NSManagedObjectContext delegate overrode fault
handling behavior to silently delete the object with ID '0xd0000000025c0006
<x-coredata://336D9F02-727B-42C5-AEDD-8007D4AB521D/Film/p151>' and substitute 
nil/0 for all property values instead of throwing.
2015-09-10 15:27:09.799 PROJECTX[2141:818058] CoreData: error: Mutating a
managed object 0xd0000000025c0006 <x-coredata://336D9F02-727B-42C5-AEDD-
8007D4AB521D/Bla/p151> (0x12812bea0) after it has been removed from its 
context.

这不会导致崩溃,但会导致应用程序行为模棱两可。

于 2015-09-10T13:38:07.727 回答
3

如果您尝试在错误的 NSManagedObjectContext 中访问 NSManagedObject,这通常会发生。如果您使用 UI 线程中的托管对象(直接显示数据,或在主线程上操作),则需要使用 NSMainQueueConcurrencyType 并发。

此外,如果您在后台线程上使用对象,它应该是具有 NSPrivateQueueConcurrencyType 并发的智利上下文。

于 2015-07-25T14:38:27.557 回答
2

查看错误的时间戳。显然,您已经尝试在其他地方访问已删除的托管对象,但很晚。汤姆和雷伊的解释是正确的。该对象不可访问,因为它不存在于持久存储中。

您应该检查您的代码中仍然可能引用托管对象的位置,并确保在它被删除后您不会尝试访问它。

于 2013-10-12T09:30:08.957 回答