首页 文章

核心数据NSFetchRequest“逻辑错误”并且短路没有结果

提问于
浏览
0

我有一个NSFetchedResultsController的查询,它以奇怪的方式失败,特别是因为它几乎与另一个应用程序中的一个相同 .

简单模型,与子Entry实体具有多对多关系的Project实体,我做过很多次的事情:

entity definition

if let proj = AppDelegate.appDelegate().persistentContainer.viewContext.object(with: projId) as? Project
{
    currentProject = proj
    let request = NSFetchRequest<Entry>(entityName:"Entry")
    request.sortDescriptors = [NSSortDescriptor(key: "entryDate", ascending: true)]
    request.predicate = NSPredicate(format: "%K = %@",#keyPath(Entry.project), proj)

    frc = NSFetchedResultsController(fetchRequest: request, 
            managedObjectContext: AppDelegate.appDelegate().persistentContainer.viewContext, 
            sectionNameKeyPath: nil, cacheName:nil)
    do {
        try frc?.performFetch()
    }
    catch
    {
        print("error for entry frc perform")
    }
}

如果我打印对象并且我可以在Base中看到关联,则performFetch无法返回任何记录,即使它们显然存在 . 如果我打开SQLite调试,我会收到以下消息“

CoreData:annotation:逻辑错误的获取请求(实体:条目;谓词:(project ==(entity:Project; id:0x604000226b40; data:{entries =“”; projectDescription = nil; projectName =“Knickers Ice”;}) ); sortDescriptors:((“(entryDate,ascending,compare :)”));类型:NSManagedObjectResultType;)短路 . CoreData:注释:总获取执行时间:0行为0.0000s .

如果我删除谓词,则返回记录,因此它显然直接归因于谓词 . 尝试了所有变种来创建它:

request.predicate = NSPredicate(format: "%K = %@","project", proj)
request.predicate = NSPredicate(format: "project = %@", proj)

等等 . 如果我搞砸了一些东西,我根本就没有看到它 .

1 回答

  • 0

    您根本不需要获取项目 . 您可以通过获取您的条目

    request.predicate = NSPredicate(format: "project.objectID = %@", projId)
    

    Edit:

    您正在尝试通过NSManagedObject过滤对象 . 我假设它导致CoreData使用:NSManagedObject仅存在于被提取的上下文中,但谓词应用得更深,其中该对象没有意义 . 所以你需要提供 objectID (但是你甚至在从上下文中检索对象之前就已经拥有它了) .

相关问题