在MagicalRecord中删除实体不是持久的 [英] Deleting entity in MagicalRecord is not persisting

查看:189
本文介绍了在MagicalRecord中删除实体不是持久的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题MagicalRecord。删除不会保留。当我删除,NSFetchedResultsControllerDelegate正确地看到该对象已被删除。

I am having a strange issue with MagicalRecord. Deletes will not persist. When I delete, NSFetchedResultsControllerDelegate correctly sees that the object has been deleted. However, if I close and reopen the app, the entity reappears.

我用来删除实体的代码是:

The code I am using to delete the entity is:

ActivityType *activityType = [_fetchedResultsController objectAtIndexPath:indexPath];
[activityType deleteInContext:[NSManagedObjectContext MR_defaultContext]];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

我用来设置NSFetchedResultsController的代码是:

The code I am using for setting up the NSFetchedResultsController is:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"ActivityType" inManagedObjectContext:[NSManagedObjectContext defaultContext]];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:[NSManagedObjectContext defaultContext] sectionNameKeyPath:nil
                                               cacheName:@"activityTypes"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

根据其他SO的帖子,我也尝试使用 [NSManagedObjectContext rootSavingContext] 在设置和删除(但没有效果)。

Based on other SO posts, I also tried to use [NSManagedObjectContext rootSavingContext] in both the setup and deletion (but to no avail).

推荐答案

我已经经历了地狱和核心数据,我学到了一些东西。 >我累了,所以我只是写一个快速总结。

I've been through hell and back with core data, and I learned a few things. I'm tired so I'll just write a quick summary.


  1. 当您删除实体时,由于您的删除规则,核心数据可能会拒绝。我的删除没有通过的原因是因为它需要级联,但它被废除。我认为它必须以某种方式把实体抛弃。我不知道为什么会是因为防止删除,但这是什么固定在我的情况。我发现它的方式是通过日志,我看到一些关于引用依赖实体的声明,我意识到删除规则将适用。

  1. When you delete an entity, core data may reject it due to your deletion rules. The reason why my deletes didn't go through is because it needed to be cascade but it was nullify. I think it has to do with somehow leaving entities abandoned. I don't know why that would be cause to prevent deletion, but that's what fixed it in my case. The way I discovered it was through log, I saw some statement about a referenced dependent entity, and I realized that delete rules will apply.

当日志说明一个严重的错误和监听器时,请检查FRC代码。因为这是侦听器,你的罪魁祸首代码将在这里。在我的情况下,我禁用[tableview beginUpdates]和[tableview endupdates]。 FRC实际上需要这个(我认为它是可选的)。否则,您会收到一些关于不一致和managedobjectcontextlistener的错误,以及如何添加或删除行等。

When the log says something about a serious error and a listener, check the FRC code. Since this is the listener, your culprit code will be here somewhere. In my case, I disabled [tableview beginUpdates] and [tableview endupdates]. The FRC actually needs this (I thought it was optional). Otherwise, you get some error about inconsistency and managedobjectcontextlistener and how rows need to be added or deleted etc. etc.

进入内存本地上下文,但可能不会保存到持久存储。这意味着FRC委托代码将看到更改,但它可能无法保存。此外,内存存储可能不会执行删除规则检查,因为它通过我的。但持久存储将进行检查。必须看看这更多。

when you delete, it may actually get saved into the memory local context, but may not get saved to the persistent store. this means that the FRC delegate code will see the changes, but it may not get saved. also, the memory store may not do the deletion rules checks as it passed mine. but the persistent store will do the checks. gotta look into this more.

这篇关于在MagicalRecord中删除实体不是持久的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆