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, of course, bug fixes. However there was this one bug that kept popping up in crash logs but that I was unable to track down. I knew it had something to do with Core Data but that’s the extend of it. 

CoreData in Denarius is implemented with two Managed Object Context, one of them being the background one for complex tasks and a second one for standard operation (as explained in this post).  So, everything was supposedly working as intended. 

However, after way too many hours I saw what was going on: I was doing a change in the main thread with information computed on the background thread… So, how did I solve it? Using objectID of course, the only safe way to transfer objects between threads. 

So instead of:

mainThreadObject.attributeX = computedThreadObjectInBackgroundQueue.attributeX

Now I had (and remember this piece of code is being executed in the background):

let transactionObjectId = mainThreadObject1.objectID
let typeObjectId = computedThreadObject2InBackgroundQueue.objectID
DispatchQueue.main.async { //Here I’m calling the main thread and push the complex background computation to the right object
      (self.managedObjectContext.object(with: transactionObjectId) as! object1).attributeX = (self.managedObjectContext.object(with: typeObjectId) as! object2).attributeX

}

A bit longer, but did the trick! This improvement will ship very soon with the new Denarius 1.6 release. 

Questions / comments? I’m at @MarcMasVi 

Marc

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: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)

        backgroundManagedObjectContext.persistentStoreCoordinator = coordinator

 

When they are initialized both contain the same data. However once changes are applied to one of them -in this example the main- like adding, changing or deleting rows, the other will not know -in this example the background/private one-.

 

Screen Shot 2016 11 27 at 13 19 12

 

Therefore if we add objects to the main managedObjectContext, when we fetch the backgroundManagedObjectContext we will not get them. 

How do we fix this? Well first we have to save the changes of the objectContext that was modified. We do so by adding once we’ve finalized the changes:

       //Commit changes

        do {

            try managedObjectContext.save()

        }catch{

            print(“ERROR: Cound not save Core Data”)

        }

        

At this point we have saved the changes in the Persistent Store. We still have to ask the backgroundObjectContext to get them from the persistent store thought. For that we will use the automatic notification “NSManagedObjectContextDidSave”.

In the class where the backgroundThread is contained we will add an observer for the notification (note that we’re passing as object the context that was modified):

//Handle changes in core data for background thread

        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.handleMainCoreDataChangeInBackgroundManagedContext(notification:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: managedObjectContext)

    }

And then we will add the function that will syncronize the backgroundThread, in this example: 

func handleMainCoreDataChangeInBackgroundManagedContext(notification: Notification){

        managedObjectContextBackgroundThread?.mergeChanges(fromContextDidSave: notification)

    }

That’s it! Now when you query the background thread you’ll get the same information as you have in the main one. 

You can read more about NSManagedObjectContext in Apple Docs

 

Questions / comments? I’m at @MarcMasVi 

 

Marc

 

 

Note: using two contexts is very powerful when used in combination with dispatchqueues

       DispatchQueue.global().async {

            //Query here in background context & save if applicable

            DispatchQueue.main.async {

                //Refresh UI & query main context & save if applicable

            }

        }

 

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:

         managedObjectContext?.undoManager = UndoManager() 

2. In the viewController where you should handle the undo operation just add:

      func undo(_ sender: AnyObject?){

  

        coreDataHelper.managedObjectContext.undoManager?.undo()

 

        refreshTable() //If applicable (for instance if you don’t use bindings)

        re-fetch the information and reload the table. This is what this

        function does in my code

 

      }

And that’s it, now you have full undo functionality! You’ll see how in the menus Undo no longer appears greyed out.

Questions / comments? I’m at @MarcMasVi 

Marc

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 fields named “gold” and being an integer.


 

We will now proceed to sort the array and store it in sortedGoldArray:

let sortedGoldArray = dwarfGoldBags.sorted { ($0.goldas Int) > ($1.goldas Int) } 

 

If instead of core data you have a multi-dimensional array it would be as easy as:

let sortArray = originalArray.sorted { ($0.[0] as Int) > ($1.[0] as Int) } 

 

In this case we’re comparing the values stored in the first value of the subArrays. 

Questions / comments? I’m at @MarcMasVi 

Marc

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 index set from the tableView selection and retrieve the relevant rows from your dataSource. From there is just a matter of composing the text to copy into the pasteboard. 

Here’s my code structure, implemented in my NSTableViewController:

func copy(sender: AnyObject?){

        

        var textToDisplayInPasteboard = “”

        let indexSet = tableView.selectedRowIndexes

        for (_, rowIndex) in indexSet.enumerate() {

            var iterator: CoreDataOjectType

            iterator=tableDataSource.objectAtIndex(rowIndex)

            textToDisplayInPasteboard = (iterator.name)! 

        }

        let pasteBoard = NSPasteboard.generalPasteboard()

        pasteBoard.clearContents()

        pasteBoard.setString(textToDisplayInPasteboard, forType:NSPasteboardTypeString)

        

    }

This will also automatically enable the Edit -> Copy (Command + C) menu, that would be disabled -grayed out- without this code. 

Questions / comments? I’m at @MarcMasVi 

Marc

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 I use a function that will return an array with the fetched NSObjectSubclass Transaction)

   private func provideAllTransactions() -> Array<Transaction>{

        let fetchRequest = NSFetchRequest(entityName: “Transaction”)

        fetchRequest.sortDescriptors = [NSSortDescriptor(key: “date”, ascending: false), NSSortDescriptor(key: “concept”, ascending: true)]

        return try! managedObjectContext.executeFetchRequest(fetchRequest) as! Array<Transaction>

    }

2. Convert to text, properly separating by commas and lines

    private func convertTransactionsToCsvString(transactions: Array<Transaction>)->String{

        var result = “HEADER 1, HEADER 2, HEADER 3, \r”//Headers

        

        for transactionIterator in transactions{ //Data

            result = result + “\”” + (transactionIterator.accountItBelongsTo?.name)! + “\”” + “,”

            result = result + “\”” + ((transactionIterator.date)?.description)! ?? NSDate().description

            result = result + “\”” + “,” + “\”” + (transactionIterator.concept)! + “\””

            result = result + “\”” + “\r”

        }

        

        return result

    }

3. Save to a path 

    func exportAllToCsv() -> Bool{

        

        let contentsToWrite = convertTransactionsToCsvString(provideAllTransactions())

        

        //Select path

        let path = “/Users/USER_NAME/Desktop/csvTest.csv”

        //Try to save it

        do {

            try contentsToWrite.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)

        }catch{

            print(“ERROR: Export could not be saved in %”, path)

        }

        

        return true

 

    }

Note that in this example the path is hardcoded to the desktop of the user named USER_NAME, you should have the user choose where he wants to save it or you can hardcode a location in your disk. 

If you would have a NSDictionary it would be almost the same but starting at point 2 with the dictionary results. 

Questions / comments? I’m at @MarcMasVi

Marc

 

PS. Here is how you would have the path be selected by the user:        

 

    privatefunc userToChooseAFileInModalPannel() -> String?{

        let pannel: NSOpenPanel = NSOpenPanel()

        pannel.canChooseDirectories = true

        pannel.canChooseFiles = false

        pannel.allowsMultipleSelection = false

        pannel.runModal()

        

        return pannel.URL?.path

    }

 then in point 3 you would do 

//Select path

        let path = userToChooseAFileInModalPannel() + “/test.csv”