在核心数据中获取 NSManagedObject 上下文时找不到 objectID 错误

objectID not found error while fetching NSManagedObject context in Core Data

我正在尝试访问基于 "objectID" 的 NSManagedObject。从我读到的内容来看,每个 NSManagedObject 都有一个唯一的 "objectID"。但是当我尝试访问基于 "objectID" 的对象时,出现以下错误。

CoreData:错误:SQLCore dispatchRequest:异常处理请求:,在 userInfo 为(null)的实体中找不到键路径对象 ID

func loadPlayerDetailsFromCoreData() {
    let fetch = Player.fetchRequest() as NSFetchRequest
    // Create Predicate
    let predicate = NSPredicate(format: "objectID == %@", selectedPlayerID!)
    fetch.predicate = predicate
    do {
        let player = try appDelegate.coreDataStack.managedObjectContext.fetch(fetch)
        print("Player selected = \(String(describing: player.first?.name))")
    } catch {
        print("Failed")
    }
}

根据objectID访问一个NSManagedObject,只需要写

appDelegate.coreDataStack.managedObjectContext.object(with: selectedPlayerID)

在这里查看Apple Doc object(with:)