核心数据fetchRequest给出executeFetchRequest:error:< null>不是有效的NSFetchRequest [英] Core data fetchRequest gives executeFetchRequest:error: <null> is not a valid NSFetchRequest

查看:45
本文介绍了核心数据fetchRequest给出executeFetchRequest:error:< null>不是有效的NSFetchRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我将自己的一个应用程序更新到iOS 10,并且可以正常运行,从那时起,我就一直在手机上使用它(安装时不删除旧应用程序,新应用程序替换了旧应用程序).现在,当我从iPhone中删除该应用程序,然后重新安装该应用程序后,它将无法正常工作.

我一直在尝试通过各种更改来解决此问题,并试图了解发生了什么问题.我还搜索了Google以及此处的Stack Overflow,但均未成功.

我得到的错误是:

由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"executeFetchRequest:错误:不是有效的NSFetchRequest."

这是我的代码,它在应用启动时非常早执行:

 让allItemFetchRequest:NSFetchRequest< Item>如果#available(iOS 10.0,*){allItemFetchRequest = Item.fetchRequest()为!NSFetchRequest< Item>} 别的 {//较早版本的后备allItemFetchRequest = NSFetchRequest(entityName:"Item")}做 {让searchItems =试试self.managedObjectContext.fetch(allItemFetchRequest)如果searchItems.count == 0 {返回真}}将let错误捕获为NSError {打印("\(#file):\(#function):\(错误):: \(error.userInfo)")} 抓住 {print("\(String(describing:self)):\(#function):致命错误")//在发布前删除致命错误()} 

并且总是在 let searchItems = try self.persistentContainer.viewContext.fetch(allItemFetchRequest)行中崩溃.

该代码用于检查自上次启动以来是否有任何数据,如果没有,我会从文件向Core Data SQLite DB添加一些数据.因此,我认为以前是可行的,因为我有iOS 10之前版本的应用程序在数据库中提供的数据.

如果我绕过此功能,并通过使用 let newItem =(NSEntityDescription.insertNewObject(forEntityName:"Item",into:self.managedObjectContext)as!Item)将数据导入数据库,则它会全部导入我的数据正确.对我来说,这似乎是我执行的第一个提取请求失败-插入似乎正常.

我正在AppDelegate内使用Apple原始Core数据堆栈功能的迁移版本.

我在做什么错了?任何帮助或提示都将不胜感激!

解决方案

编辑

当我简化fetchRequest的声明时,它可以正常工作.而不是使用:

 让allItemFetchRequest:NSFetchRequest< Item>如果#available(iOS 10.0,*){allItemFetchRequest = Item.fetchRequest()为!NSFetchRequest< Item>} 别的 {//较早版本的后备allItemFetchRequest = NSFetchRequest(entityName:"Item")} 

尝试使用:

 让entityName = String(描述:Item.self)let request = NSFetchRequest< Item>(entityName:实体名称) 

干杯!

I while ago, I updated one of my apps to iOS 10, and I got it working, and since then I've been using it on my phone (installed without removing old app, where new app replaced old one). Now when I've removed the app from my iPhone, and reinstalled the same one it doesn't work.

I've been trying out to solve this problem by different changes and trying to get an understanding on what is going wrong. I also searched on Google and here on Stack Overflow without any success.

The error I get is this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: is not a valid NSFetchRequest.'

Here is my code, and it is performed very early in the app launch:

        let allItemFetchRequest: NSFetchRequest<Item>
    if #available(iOS 10.0, *) {
        allItemFetchRequest = Item.fetchRequest() as! NSFetchRequest<Item>
    } else {
        // Fallback on earlier versions
        allItemFetchRequest = NSFetchRequest(entityName:"Item")
    }

    do {
        let searchItems = try self.managedObjectContext.fetch(allItemFetchRequest)

        if searchItems.count == 0 {
            return true
        }
    } catch let error as NSError {
        print("\(#file):\(#function): \(error)::\(error.userInfo)")
    } catch {
        print("\(String(describing: self)):\(#function): fatal error")
        // Remove before release
        fatalError()
    }

And it's always crashing at the let searchItems = try self.persistentContainer.viewContext.fetch(allItemFetchRequest) row.

The code is used to check if there is any data since previous launch, and if not, I add a some data to Core Data SQLite DB from a file. So, previously if worked because I had data in the DB from a pre-iOS 10 version app, I think.

If I bypass this functionality and imports data into DB by using let newItem = (NSEntityDescription.insertNewObject(forEntityName: "Item", into: self.managedObjectContext) as! Item) it does import all my data correctly. To me it feels like it is the first fetch request I do that fails - inserts seems to work ok.

I'm using an migrated version of the Apple-original Core data stack functions inside the AppDelegate.

What am I doing wrong? Any help or hints would be appreciated!

解决方案

Edit

I got It working when I simplified fetchRequest's declaration. Instead of using:

let allItemFetchRequest: NSFetchRequest<Item>
if #available(iOS 10.0, *) {
    allItemFetchRequest = Item.fetchRequest() as! NSFetchRequest<Item>
} else {
    // Fallback on earlier versions
    allItemFetchRequest = NSFetchRequest(entityName:"Item")
}

Try using:

let entityName = String(describing: Item.self)
let request = NSFetchRequest<Item>(entityName: entityName)

Cheers!

这篇关于核心数据fetchRequest给出executeFetchRequest:error:&lt; null&gt;不是有效的NSFetchRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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