0

I try to run the iOS app on Mac Catalyst, but my app has Core Data. I have read this post:

Initializing CoreData in macOS

But I do not achieve to implement it. I don't know...

I write this code in a View Controller where a use Core Data. It is the same code that exists in AppDelegate.swift file (I haven't deleted it).

#if targetEnvironment(macCatalyst)
// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {

     let container = NSPersistentContainer(name: "MyApp")

     container.loadPersistentStores(completionHandler: { (storeDescription, error) in
         if let error = error as NSError? {
         print("Unresolved error \(error), \(error.userInfo)")
     }
   })
 return container
}()

// MARK: - Core Data Saving support
func saveContext() {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
            let nserror = error as NSError
            print("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}
#endif

And when I update or insert data in Core Data:

// SAVE the context.
do {
    try context.save()
} catch {
    let nserror = error as NSError
    print("Unresolved error \(nserror), \(nserror.userInfo)")
}

#if targetEnvironment(macCatalyst)
// SAVE the context.
self.saveContext()
#endif

I am very lost. I need to know how to write the code. I know that it is not complicated, but I am lost.

I SHOW THE CODE I USE TO READ/WRITE Core Data.

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let context = appDelegate.persistentContainer.viewContext

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Topic")

fetchRequest.returnsObjectsAsFaults = false

let results = try? context.fetch(fetchRequest)

let newSelectedValue = string

if (results?.count)! > 0 {
    for updateItem in results as! [NSManagedObject] {
        updateItem.setValue(newSelectedValue, forKey: "topicName")
    }
} else {
    let newItem = Topic(context: context)
    newItem.topicName = newSelectedValue
}

// SAVE the context.
do {
    try context.save()
} catch {
    let nserror = error as NSError
    print("Unresolved error \(nserror), \(nserror.userInfo)")
}
Markus
  • 1,147
  • 16
  • 26

1 Answers1

0

From what I understand, the post you read confused you more than it helped you. What it suggests is to move Core Data code from AppDelegate to the main ViewController, something I personally disagree with.

When using Catalyst, you can use exactly the same code for Core Data.

So what I suggest is to delete the code you made specifically for macCatalyst, and load PersistentStores for both iOS and macOS in the same place.

  • I partially understand what it says. I also do not think it is very correct to move the Core Data methods from the AppDelegate.swift to another View Controller: it seems to me a "crappy" solution But I get lost in your phrase: "load PersistentStores" I don't quite understand (it's my fault) – Markus Nov 08 '20 at 21:07
  • I modify the question to attach the code I use to write / read Core Data. – Markus Nov 08 '20 at 21:07