Post

Replies

Boosts

Views

Activity

Reply to Xcode 12 / iOS14 Core Data setup
Here is how you do it: Add this code to your AppDelegate file: lazy var persistentContainer: NSPersistentContainer = {      let container = NSPersistentContainer(name: "Your_App_CoreData_Model_Name")     container.loadPersistentStores(completionHandler: { (storeDescription, error) in        if let error = error as NSError? {          fatalError("Unresolved error \(error), \(error.userInfo)")       }     })            return container   }()     func saveContext() { let context = persistentContainer.viewContext      if context.hasChanges {        do {          try context.save()       } catch {          let nserror = error as NSError          fatalError("Unresolved error \(nserror), \(nserror.userInfo)")       }     }   } func managedObjectContext() -> NSManagedObjectContext {      let managedObjectContext = ModelManager.instance.persistentContainer.viewContext            return managedObjectContext   }
Sep ’20
Reply to Issue with Core Data not finding the correct entity
You can get this error several different ways. If you get it during fetch requests then you should try this: @FetchRequest(entity: NSEntityDescription.entity(forEntityName: "Your Entity Name", in: managedObjectContext), sortDescriptors: [NSSortDescriptor(key: "Your Key", ascending: true)]) In dynamic fetch requests you should try this: var entity: Entity var fetchRequest: FetchRequest<Your Entity> init(_ entity: Entity) { self.entity = entity self.fetchRequest = FetchRequest(entity: NSEntityDescription.entity(forEntityName: "Your Entity", in: managedObjectContext), sortDescriptors: [NSSortDescriptor(key: "Your Key", ascending: true)], predicate: NSPredicate(format: "entity.uniqueIdentifier == %@", entity.uniqueIdentifier!)) } var youEntities: FetchedResults<Your Entity> { fetchRequest.wrappedValue } When creating core data objects: let entityDescription = NSEntityDescription.entity(forEntityName: "Your Entity", in: managedObjectContext) let object = Your Entity(entity: entityDescription!, insertInto: managedObjectContext) Hope this helps for your Core Data with SwiftUI issues.
Sep ’20