与此上下文的 CoreData SwiftUI 错误不同的 NSManagedObjectModel
Posted
技术标签:
【中文标题】与此上下文的 CoreData SwiftUI 错误不同的 NSManagedObjectModel【英文标题】:different NSManagedObjectModel than this context's' CoreData SwiftUI error 【发布时间】:2021-04-01 07:41:58 【问题描述】:我之前刚刚为我的应用程序设置了 CoreData 来存储一些数据。我最初让它与不同的实体一起工作,但很快意识到它是错误的模型,并创建了一个新实体并删除了最初的实体。在我的 CoreDataManager 类中更改适当的变量/对象名称后,我一直遇到以下错误(线程问题)
'The fetch request's entity 0x600001040370 'UserSettings' appears to be from a different NSManagedObjectModel than this context's'
terminating with uncaught exception of type NSException
在这里给出一些上下文是我的 coreDataManager 类:
import Foundation
import UIKit
import CoreData
class CoreDataManager
static let shared: CoreDataManager =
let appDelegate = AppDelegate.instance!
let instance = CoreDataManager(managedObjectContext: appDelegate.persistanceContainer.viewContext)
return instance
()
var managedContext: NSManagedObjectContext
init(managedObjectContext: NSManagedObjectContext)
managedContext = managedObjectContext
//MARK:- CalorieTracker Insert/Update/Delete
extension CoreDataManager
func addUserSettingsData(id: UUID, firstName: String, height: Double, weight: Double, gender: String, age: Double, activityLevel: String, bmr: Double, completionHandler: @escaping (_ succeed: Bool, _ error: Error?) -> Void)
let userSettingsEntity = NSEntityDescription.insertNewObject(forEntityName: "UserSettings", into: managedContext) as? UserSettings
userSettingsEntity?.id = id
userSettingsEntity?.firstName = firstName
userSettingsEntity?.height = height
userSettingsEntity?.weight = weight
userSettingsEntity?.gender = gender
userSettingsEntity?.age = age
userSettingsEntity?.activityLevel = activityLevel
userSettingsEntity?.bmr = bmr
do
try managedContext.save()
completionHandler(true, nil)
catch let error
completionHandler(false, error)
func fetchCalorieTrackerData(completionHandler: @escaping (_ succeed: Any?, _ error: Error?) -> Void)
var progresses = [UserSettings]()
let progressRequest: NSFetchRequest<UserSettings> = NSFetchRequest<UserSettings>(entityName: "UserSettings")
do
progresses = try managedContext.fetch(progressRequest)
completionHandler(progresses, nil)
catch let error
completionHandler(nil, error)
我认为问题出在我的 fetch 请求中,因为在我的 viewModel 中创建实体时会执行 print 语句:
import Foundation
class AddViewModel: ObservableObject
@Published var userSettings = [UserSettings]()
init()
fetchProgresses()
//Creating the data
func addCalorieTrackerDate(id: UUID, firstName: String, height: Double, weight: Double, gender: String, age: Double, activityLevel: String, bmr: Double)
CoreDataManager.shared.addUserSettingsData(id: id, firstName: firstName, height: height, weight: weight, gender: gender, age: age, activityLevel: activityLevel, bmr: bmr) (isAdded, error) in
if let error = error
print(error)
else
print("Data has been added!")
print(gender)
//fetching the data
func fetchProgresses()
CoreDataManager.shared.fetchCalorieTrackerData (userSettings, error) in
if let error = error
print(error)
if let userSettings = userSettings as? [UserSettings]
self.userSettings = userSettings
我已经被这个错误困住了很长一段时间,任何帮助将不胜感激!
【问题讨论】:
您应该在 CoreDataManager 中将 init 方法设为私有,这样您就不会错误地创建单独的实例。 【参考方案1】:我的程序的问题是在我的持久性文件中我使用了错误的名称。我没有使用 CoreData 类,而是使用了项目名称。正确的是:
container = NSPersistentContainer(name: "CalorieTracker")
【讨论】:
以上是关于与此上下文的 CoreData SwiftUI 错误不同的 NSManagedObjectModel的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在 SwiftUI 中重新打开应用程序时没有保存 CoreData?
最新Xcode 13.3RC对SwiftUI和CoreData中两个恼人问题的修复
最新Xcode 13.3RC对SwiftUI和CoreData中两个恼人问题的修复
在 SwiftUI 视图中发布后台上下文 Core Data 更改而不阻塞 UI