David Koenig's Blog

CoreData Functions - Cocoa


In this article, I would like to go into the functions that should be created to use CoreData in Cocoa (macOS). We use an Entity that I named Contact!

We create a Swift file which we call CoreDataHandler.swift:

import Cocoa import CoreData class CoreDataHandler: NSObject { //1 Save and Fetch private class func getContext() -> NSManagedObjectContext { let appDelegate = NSApplication.shared.delegate as! AppDelegate return appDelegate.persistentContainer.viewContext } class func saveObject(name: String, phone: String, address: String) -> Bool { let context = getContext() let entity = NSEntityDescription.entity(forEntityName: "Contact", in: context) let managedObject = NSManagedObject(entity: entity!, insertInto: context) managedObject.setValue(name, forKey: "name") managedObject.setValue(phone, forKey: "phone") managedObject.setValue(address, forKey: "address") do { try context.save() print("saved") return true } catch { print("did not save") return false } } class func fetchObject() -> [Contact]? { let context = getContext() var contact: [Contact]? = nil do { contact = try context.fetch(Contact.fetchRequest()) return contact } catch { return contact } } //2 Delete One or Delete All class func deleteObject(contact: Contact) -> Bool { let context = getContext() context.delete(contact) do { try context.save() print("deleted one") return true } catch { return false } } class func cleanDelete() -> Bool { let context = getContext() let delete = NSBatchDeleteRequest(fetchRequest: Contact.fetchRequest()) do { try context.execute(delete) print("deleted all") return true } catch { return false } } //3 Filter Data class func filterData() -> [Contact]? { let context = getContext() let fetchRequest : NSFetchRequest = Contact.fetchRequest() var contact : [Contact]? = nil var predicate = NSPredicate(format: "name contains[c] %@", "Jacob") fetchRequest.predicate = predicate do { contact = try context.fetch(fetchRequest) return contact } catch { return contact } } }

(1) At the beginning of the NSManagedObjectContext, here we establish the connection to the AppDelegate and reflect the context of the persistent controller. It all runs in the background and has become a lot easier since Swift 4.x. And as you can see, it's one Class function.

This is where it gets really exciting, here we access the core data file in detail. At the beginning there is context again as a constant and we access the getContext() function we created. The second constant accesses our "Contact" entity and creates a connection. The NSManagedObject is then the higher-level connection to our attributes ("name"; "phone"; "address"). So we have established a connection to the core data file. In the do - catch syntax, we first get to store the data in the entity and then in the attributes. Here, too, we use the class function to access core data with a -> Bool!

The class function fetchObject is simpler. Again we have a context Constant. Here, however, you access the array of the entity "Contact", which is set as the default value "nil". The great thing since Swift macOS 10.13 is that Swift automatically recognizes the "Contact" entity. Again we have a do catch statement. We do not save anything here, but retrieve the data with fetchRequest().

(2) Every contact we create has an index in the array of "Contacts". You access the context and delete a row in the "Contact" entity. In the end you save again. Here too you use the class function which shows a -> Bool. So true or false.

The second delete function, deletes all entries in the entity. For this we use the NSBatchDeleteRequest (...) provided by Apple. This time we do not save, but execute the constant "delete".

(3) Here you can see the filterData() function, another class function. This time accesses the array of the Contact entity. Resembles the others Functions. With one exception, the NSPredicate (...)! In the variable "predicate" we enter the parameter we are looking for. In this case for the name Jacob.

David Koenig