简体   繁体   English

无法使用RestKit 0.20从Core Data中删除实体

[英]Can not delete an entity from Core Data, using RestKit 0.20

The code below does not delete the entity. 下面的代码不会删除实体。 The "delete was successful" message appears on the console so the entity is found. 控制台上出现“删除成功”消息,因此找到了实体。 All other operations I use succeed. 我使用的所有其他操作都成功。

I am using RestKit 0.20. 我正在使用RestKit 0.20。

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *error = nil;

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Auction" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"AuctionID = %d", auctionID];
[fetchRequest setPredicate:predicate];

NSArray *result = [context executeFetchRequest:fetchRequest error:&error];
if(result.count) {
    Auction *block = result[0];
    [context deleteObject:block];
    BOOL status = [context save:&error];
    if (status == NO) {
        NSLog(@"delete falied for AuctionID:%d, error: %@", auctionID, error);
    }
    else {
        [context processPendingChanges];
        NSLog(@"delete was successful for AuctionID:%d", auctionID);

    }
}

Why might this delete operation not succeed and what is the solution to making it work. 为什么删除操作不成功以及使其工作的解决方案是什么。

I found this solution : 我发现这个解决方案:

In fact, you have to fetch datas from the persistentstore and not the current created managed context : 实际上,您必须从persistentstore获取数据,而不是当前创建的托管上下文:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyModel"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];

// Setup fetched results
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                        managedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

// AND TO DELETE A MODEL :

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:myobject];

i am doing the same thing and have nearly same code. 我正在做同样的事情,并有几乎相同的代码。 In my code also, i get delete done and saved.... 在我的代码中,我删除完成并保存....

But, its not deleted when i am checking DB. 但是,当我检查数据库时,它没有被删除。

the problem is not with simulator... SURE bcz i am getting same problem on device also. 问题不在于模拟器... SURE bcz我也在设备上遇到同样的问题。 there is something called child context, it might be the cause...Check these links http://restkit.org/api/0.20.0-dev/Classes/RKManagedObjectRequestOperation.html#//api/name/managedObjectContext RestKit 0.20 — What is the preferred way to create a new NSManagedObject? 有一个叫做子上下文的东西,它可能是原因......检查这些链接http://restkit.org/api/0.20.0-dev/Classes/RKManagedObjectRequestOperation.html#//api/name/managedObjectContext RestKit 0.20 -创建新NSManagedObject的首选方法是什么? . If you found solution pls share here 如果你找到解决方案请在这里分享

@Sumitiscreative I ran into the same issue today. @Sumitiscreative我今天遇到了同样的问题。 What if found was that normally using Core Data you have to use 如果发现通常使用您必须使用的核心数据

[NSManagedObject save:] 

for it to store the changes. 用于存储更改。 I dug through Restkit a bit and found this 我挖了一下Restkit,发现了这一点

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:(NSManagedObject *)];
[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext saveToPersistantStore:(NSError *)];

Calling this after the above delete method is working to remove the object out of the DB. 在上述删除方法之后调用此方法正在将对象从数据库中删除。

**Edit - Also I would have just made this a comment but i don't have the option **编辑 - 我也只是发表评论,但我没有选择

@Lance Hey, pls update your restkit with the latest version. @Lance Hey,请用最新版本更新你的restkit。 As now, this works in the latest version , if your server related configuration is correct. 现在,如果您的服务器相关配置正确,这可以在最新版本中使用。 and if you get success codes for your delete request from server. 如果您从服务器获取删除请求的成功代码。 Then, restkit automatically deletes the data. 然后,restkit会自动删除数据。

If you need to delete any data externally then, you can use persistentStoreManagedObjectContext and after deleting, save it. 如果需要在外部删除任何数据,则可以使用persistentStoreManagedObjectContext,删除后保存。

Also, if you want to check at your end that whether its correctly deleting via restkit or not. 此外,如果您想在最后检查是否通过restkit正确删除。 what you can do is ... 你能做的是......

make delete request, after success check with same id, if item exists. 如果项目存在,则在成功检查后使用相同的ID进行删除请求。 (just for help) (只是为了帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM