gpt4 book ai didi

objective-c - 如何从 NSManagedObjectContext 中删除新对象

转载 作者:行者123 更新时间:2023-12-04 05:51:43 26 4
gpt4 key购买 nike

好的,让我们从代码开始。我正在遍历返回的字典数组并基于它们创建(或更新)对象。在这种方法中,我正在尝试查找或创建一个新实体。然后,如果该对象应该被删除,我想这样做,而不是浪费时间用新信息更新它。

- (void)updateOrCreateObjectWith:(NSDictionary*)dictionary {
RKManagedObjectStore *objectStore = ((MyAppDelegate*)[[UIApplication sharedApplication] delegate]).objectStore;

id updateObject = (NSManagedObject*)[objectStore findOrCreateInstanceOfEntity:[resource entity] withPrimaryKeyAttribute:@"myID" andValue:[dictionary objectForKey:@"id"]];

[updateObject setMyID:[dictionary objectForKey:@"id"]];

// if marked for deletion, delete it now
if ([[dictionary objectForKey:@"deleted_at"] isKindOfClass:[NSString class]]) {
if ([updateObject isNew]){
NSError *error = nil;
[objectStore.managedObjectContext save:&error];
if (error) {
NSLog(@"error saving before delete: %@",error);
return;
}
// [objectStore.managedObjectContext deleteObject:updateObject];
// [objectStore.managedObjectCache delete:updateObject];
}
else {
[objectStore.managedObjectContext deleteObject:updateObject];
}
return;
}

[updateObject updateWith:dictionary];
}

要注意的部分是 deleted_at 部分,其中包含(1)保存部分,(2)从上下文中删除对象,以及(3)从缓存中删除对象。我已经尝试了这三种的一些组合,但我没有得到想要的结果。

如果我从缓存中删除它(只是#3):
  • 对象被保存,但它们没有属性。

  • 如果我从托管上下文中删除它(只是#2),我会得到:
    NSUnderlyingException=Cannot update object that was never inserted.

    由于它从未插入,我想我会保存它然后删除它(#1和#2),但后来我得到:
    *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xed27810 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/User/p166>''

    那么从 NSMagedObjectContext 中删除"new"对象的正确方法是什么?

    最佳答案

    更容易获得 RKManagedObjectStore [[RKObjectManager sharedManager] objectStore] 的实例(假设您想要共享的,因为您正在调用您的应用程序委托(delegate),所以您似乎这样做了)。

    检查 deleted_at创建 NSManagedObject 之前的 key 。此代码假定您要转换为类型 Resource ,它是 NSManagedObject 的子类.它未经测试,但应该让你知道你应该做什么。

    - (void)updateOrCreateObjectWith:(NSDictionary*)dictionary {
    RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] objectStore];

    //get a reference to the object
    Resource *resource = [Resource findFirstByAttribute:@"myID" withValue:[dictionary objectForKey:@"id"]];

    //see if "deleted_at" exists in dictionary
    if ([[dictionary objectForKey:@"deleted_at"] isKindOfClass:[NSString class]])
    {
    //check to see if object exists in the context
    if(resource)
    {
    //if it exists, delete it
    [objectStore.managedObjectContext deleteObject:resource];
    }
    } else {
    //no "deleted at", so create the object
    if (!resource) {
    //resource is nil (it doesn't exist in the context), so we need to create it
    resource = [Resource object];
    }
    [resource updateWith:dictionary];
    }

    NSError *error = nil;
    [objectStore.managedObjectContext save:&error];
    if (error) {
    NSLog(@"error saving before delete: %@",error);
    }
    }

    关于objective-c - 如何从 NSManagedObjectContext 中删除新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9987220/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com