I did a post a while back about how to enable undo functionality when using CoreData. It continues to work great for AppKit or UIKit apps, but what about if you’ve adopted SwiftUI as Apple is suggesting we do? Well… unfortunately the previous approach won’t work quite as well. After a few hours working this, …
Tag Archives: coredata
Using NSSortDescriptor to sort Dates and nil values
Today I’ve been working on sorting tasks, as I’m using swiftUI and CoreData I use a FetchRequest with the following SortDescriptor: NSSortDescriptor(keyPath: \Task.targetDate, ascending: true) The idea being it will sort based on the task due date from smallest to the largest, meaning you’ll see the ones you should act on first at the top. …
Continue reading “Using NSSortDescriptor to sort Dates and nil values”
Multiple Word, Random Order Search – CoreData & SwiftUI
Today I worked on the search functionality for the upcoming Product Management app. Given that this app will contain lots of long notes, having good search is essential. In most cases & for most apps, a simple one to one comparison will do the job great -like in the above image-. Here’s how a predicate implementation …
Continue reading “Multiple Word, Random Order Search – CoreData & SwiftUI”
Counting CoreData entries based on relationship entities
As I continue working on the new product management app, I was trying to get a quick CoreData count. To be precise, I needed to get the count for how many of its relationships met a given criteria. There’s quite a few approaches but none as clean as “the filter”: Using it for boolean arguments: …
Continue reading “Counting CoreData entries based on relationship entities”
Denarius 1.6 & the slippery bug
This last couple of months I’ve been hard at work on two new projects, the first of which will be released fairly soon! But, today I wanted to write about something else: CoreData Concurrency. Since the release of Denarius about a year ago I’ve been regularly improving the personal finance app with new features and, …
Synchronizing Main and Background Core Data Threads (Swift 3)
Let’s say we have two different managedObjectContext (with one persistentStoreCoordinator). The first one is used across the app for most quick fetches: var mainManagedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType) mainManagedObjectContext.persistentStoreCoordinator = coordinator And the second, running in the background, for the queries that take a long time: var backgroundManagedObjectContext = NSManagedObjectContext(concurrencyType: …
Continue reading “Synchronizing Main and Background Core Data Threads (Swift 3)”
Undo functionality in Core Data (Swift 3)
If you’re developing a Core Data based application for Mac, wouldn’t it be great if you could add undo support? Well, turns out that is a couple of lines away. Follow the simple steps: 1. Make sure your managedObjectContext has an undo manager, without it Core Data can’t keep track of the changes: …
Continue reading “Undo functionality in Core Data (Swift 3)”
Sorting Multi Dimensional Array (Swift 3)
Hello all, This one I’ve found specially useful when working with Core Data fetched arrays. Let’s say that we want to sort the result of a fetch request based on the value of the field “gold”, we would therefore do the following: let dwarfGoldBags = … //Contains the result of a fetch request with one of the …
Continue reading “Sorting Multi Dimensional Array (Swift 3)”
Copy one or multiple NSTableView rows, Swift
So here’s a simple yet tricky one: you’ve created your NSTableView but now you would like to allow a user to copy to the clipboard one, or a couple of rows. How do you do it? 1. Implement the function copy (func copy(sender: AnyObject?){}), do not confuse with the method for duplicating an object. 2. Get the …
Continue reading “Copy one or multiple NSTableView rows, Swift”
Easy Core Data CSV Exporter in Swift
A new app I’m working on has a Core Data back end and I wanted to implement a quick way to export all the entities into a CSV file and dump it into the Desktop. Here are the snippets to do it: 1. Make sure you get the NSObjects you want to export (in this case …