Hi,
I have the following coredata singleton class
in my appdelegate i try to update the data from json
but in my app when it launches i get different error messages regarding thread or observer of NSManagedObjectContextObjectsDidChangeNotification
or Incorrect guard value: or Main Thread Checker: UI API called
what is the problem and what changes should i make for it to work?
thanks
in my appdelegate:
I have the following coredata singleton class
in my appdelegate i try to update the data from json
but in my app when it launches i get different error messages regarding thread or observer of NSManagedObjectContextObjectsDidChangeNotification
or Incorrect guard value: or Main Thread Checker: UI API called
what is the problem and what changes should i make for it to work?
thanks
Code Block import CoreData import Foundation class CoreDataStack { static let sharedManager = CoreDataStack() private init() {} // Prevent clients from creating another instance. //This is the name of your coreData Database static let modelName = "myDB" static let FirstLaunchKey = "firstLaunch" lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = Bundle.main.url(forResource: CoreDataStack.modelName, withExtension: "momd")! return NSManagedObjectModel(contentsOf: modelURL)! }() lazy var applicationDocumentsDirectory: URL = { return NSPersistentContainer.defaultDirectoryURL() }() lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let persistentStoreURL = self.applicationDocumentsDirectory.appendingPathComponent(CoreDataStack.modelName + ".sqlite") do { // let dict = [NSSQLitePragmasOption: ["journal_mode":"DELETE"]] try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: persistentStoreURL, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: false]) } catch { fatalError("Persistent store error! \(error)") } return coordinator }() fileprivate lazy var saveManagedObjectContext: NSManagedObjectContext = { let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) moc.persistentStoreCoordinator = self.persistentStoreCoordinator return moc }() @objc lazy var mainObjectContext: NSManagedObjectContext = { let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) managedObjectContext.parent = self.saveManagedObjectContext return managedObjectContext }() func saveMainContext() { guard mainObjectContext.hasChanges || saveManagedObjectContext.hasChanges else { return } mainObjectContext.performAndWait() { // print("save performAndWait") do { try self.mainObjectContext.save() } catch { fatalError("Error saving main managed object context! \(error)") } } saveManagedObjectContext.perform() { // print("save simple") do { try self.saveManagedObjectContext.save() } catch { fatalError("Error saving private managed object context! \(error)") } } } func removeDatabaseForVersion(version:String){ let previouslyVersion = UserDefaults.standard.bool(forKey: version) if !previouslyVersion { UserDefaults.standard.set(true, forKey: version) // Default directory where the CoreDataStack will store its files let directory = NSPersistentContainer.defaultDirectoryURL() let url = directory.appendingPathComponent(CoreDataStack.modelName + ".sqlite") let shmURL = directory.appendingPathComponent(CoreDataStack.modelName + ".sqlite-shm") let walURL = directory.appendingPathComponent(CoreDataStack.modelName + ".sqlite-wal") _ = try? FileManager.default.removeItem(at: url) _ = try? FileManager.default.removeItem(at: shmURL) _ = try? FileManager.default.removeItem(at: walURL) } } } [update db class](https://developer.apple.com/forums/content/attachment/31e8ff0d-abb3-4fb7-a346-c05f8a610b23){: .log-attachment}
in my appdelegate:
Code Block UpdateDbClass.updateDatabase(entityName: DbTable.VehiclesEntity.rawValue, completionHandler: { print(" DB updated delegate") })