加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 使用“未知命令类型”执行NSBatchDeleteRequest时应用

发布时间:2020-12-14 02:23:51 所属栏目:百科 来源:网络整理
导读:我正在编写OS X应用程序,它需要能够删除其Core Data存储中保存的所有“SongEntity”实例. 但是,当我尝试执行NSBatchDeleteRequest时,我的应用程序崩溃,使用以下控制台输出(摘录): Unknown command type (entity: SongEntity; predicate: ((null)); sortDesc
我正在编写OS X应用程序,它需要能够删除其Core Data存储中保存的所有“SongEntity”实例.
但是,当我尝试执行NSBatchDeleteRequest时,我的应用程序崩溃,使用以下控制台输出(摘录):

Unknown command type (entity: SongEntity; predicate:
((null)); sortDescriptors: ((null)); type:
NSManagedObjectIDResultType; ) >

这是我的实现:

func clearStore()
{
    let fetchRequest = NSFetchRequest(entityName: "SongEntity")
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

    do
    {
        try managedObjectContext.executeRequest(deleteRequest)
    }
    catch
    {
        fatalError("Not able to perform operation: (error)")
    }
    managedObjectContext.reset()
}

任何帮助将非常感激

编辑:
事实证明,此问题与所选的商店类型有关:
从类NSBatchDeleteRequest:

//  May not be supported by all store types.

我尝试将存储类型从NSXMLStoreType(默认为macOS模板)更改为NSSQLiteStoreType,现在它可以正常工作.

NSBatchDeleteRequest在持久性存储协调器上执行,而不是在托管对象上下文上执行.
try persistentStoreCoordinator.executeFetchRequest(
    batchDeleteRequest,withContext:context
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读