• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - NSFetchedResultsController 全部删除

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:06:17 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

AcaniChat 与原生 iPhone 消息应用程序一样,具有清除所有消息的方法:

for (Message *message in [fetchedResultsController_ fetchedObjects]) {
    [managedObjectContext_ deleteObject:message];
}
if (![managedObjectContext_ save:&error]) {
    // TODO: Handle the error appropriately.
    NSLog(@"Delete message error %@, %@", error, [error userInfo]);
}

但是,只有当有大量消息被删除(足以使某些消息不在屏幕上)时,AcaniChat 才会崩溃并出现错误:

2011-04-10 01:06:17.629 AcaniChat[11129:207] Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  CoreData could not fulfill a fault for '0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42>' with userInfo {
    NSAffectedObjectsErrorKey =     (
        "<Message: 0x4d665b0> (entity: Message; id: 0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42> ; data: <fault>)"
    );
}
2011-04-10 01:06:17.721 AcaniChat[11129:207] Delete message error Error Domain=NSCocoaErrorDomain Code=133000 "The operation couldn’t be completed. (Cocoa error 133000.)" UserInfo=0x4d9cf70 {NSAffectedObjectsErrorKey=(
    "<Message: 0x4d665b0> (entity: Message; id: 0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42> ; data: <fault>)"
), NSUnderlyingException=CoreData could not fulfill a fault for '0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42>'}, {
    NSAffectedObjectsErrorKey =     (
        "<Message: 0x4d665b0> (entity: Message; id: 0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42> ; data: <fault>)"
    );
    NSUnderlyingException = "CoreData could not fulfill a fault for '0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42>'";
}
2011-04-10 01:06:18.036 AcaniChat[11129:207] CoreAnimation: ignoring exception: CoreData could not fulfill a fault for '0x4d7d050 <x-coredata://F07CAEF9-5F38-4A1F-BA17-52A3E2E56978/Message/p42>'

我该如何解决这个问题?



Best Answer-推荐答案


我通过设置 fetchedResultsController_.delegate = nil 解决了这个问题,删除了 cellMap 中的所有对象(这实际上是 UITableView *chatContent 因为我将 NSDate 对象添加到 cellMap 用于时间戳单元格),然后设置 fetchedResultsController_.delegate = self 并执行再次获取请求。

NSError *error;
fetchedResultsController.delegate = nil;               // turn off delegate callbacks
for (Message *message in [fetchedResultsController fetchedObjects]) {
    [managedObjectContext deleteObject:message];
}
if (![managedObjectContext save:&error]) {
    // TODO: Handle the error appropriately.
    NSLog(@"Delete message error %@, %@", error, [error userInfo]);
}
fetchedResultsController.delegate = self;              // reconnect after mass delete
if (![fetchedResultsController performFetch:&error]) { // resync controller
    // TODO: Handle the error appropriately.
    NSLog(@"fetchMessages error %@, %@", error, [error userInfo]);
}
[cellMap removeAllObjects];
[chatContent reloadData];

关于ios - NSFetchedResultsController 全部删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609921/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap