CoreDataStack: easy use of CoreData framework with Swift

CoreData framework

The CoreData framework is the standard framework provided by Apple to manage persistent data within the applications. This framework is based on an object graph model.

The CoreData framework is a master piece of the iOS applications, it helps you build the model layer of your applications and handles for you a lot of work to persist the state of the model objects to disk.

To have more informations about the CoreData framework see Apple’s documentation.

Don’t repeat yourself

Developing iOS projects which use the CoreData framework often leads to writing the same code pattern:

To reduce the portion of code to produce I wrote a small framework which handles the initial CoreData stack (NSManagedObjectModel and NSPersistentStoreCoordinator objects) and the NSManagedObjectContext objects (to fetch and create NSManagedObject objects).

The CoreDataStack framework is available at Github: An example is present to show you how to use the framework.

Create the initial CoreData stack

The CoreDataStack class provides a useful init method to set up the initial CoreData stack: just provide an array of the names of the model files (the .momd files) and the name of the file to save the persistent data (the created file is into the “Application Support” directory of the application).

The init method has many optional parameters, see the code source to have more informations:

The NSManagedObjectContext objects

The CoreDataStack class handles two NSManagedObjectContext objects:

  • the first context is the writer, which exclusively writes into the persistent store coordinator, it is not bound to the main thread. Write to disk is an expensive operation, so it must not block the main thread.
  • the second is the default context. As it is bound to the main thread, you use it to fetch the objects from the NSFetchedResultsController.

These two NSManagedObjectContext objects are bound to the same NSPersistentStoreCoordinator object.

The batch NSManagedObjectContext objects are bound to a different NSPersistentStoreCoordinator object to avoid locking the first NSPersistentStoreCoordinator object.


Fetch NSManagedObject objects

Use the default context from your CoreDataStack object with your NSFetchedResultsController objects:

Perform actions on NSManagedObject objects

To perform actions on NSManagedObject objects, the CoreDataStack class provides three useful methods:

  • The performBlockInContext method is used to perform a few actions like adding or deleting a very small amount of objects. As it uses a context bound to the default context, the changes are immediately passed to the default context when it saved, but the main thread is blocked.

  • The performBlockInContextForBackgroundTask method is used to perform a few actions too but here save the context does not block the main thread. The changes are not passed to the default context unless you call the saveBlock block with its parameter to true.

  • The performBlockInContextForBatchTask method is used to perform heavy actions like adding or deleting thousands objects. The method provides a saveBlock block which you can run into the first block to save the context. To see the changes in the default context, you have to refetch the requests.

As you can see, with these three methods, the first block provides a NSManagedObjectContext object to perform actions on NSManagedObject objects. This context is not bind to the main thread to preserve the user interface responsiveness. At the end of the first block, the context is automatically saved.

The second block (named mainThreadBlock) is optional and is executed in the main thread: you can perform here UI actions like reloading a UITableView or a NSFetchedResultsController object.

Handle yourself the contexts

If you want to handle yourself the NSManagedObjectContext object and save it, get a context object from one of the first methods below and save it with the third method:

Save the contexts

You can save the default and the writer contexts of the CoreDataStack object at every moment with the method:

To save a NSManagedObjectContext object generated by the CoreDataStack object call the method:

Get the framework

I hope this framework will help you to use the CoreData framework. It is available at Github: Read the example to know how to use the framework.

How to know which ports of a server are opened?

As I forget always how to know which ports are opened on a server (on the local machine), I write down here the command to run:

This is the result:

Useful aliases for Docker on OSX

After downloading and installing docker toolbox, many docker tools will be present on your OSX : docker engine, docker-machine, docker-compose. These are aliases that I am using to work with the docker tools on OSX.

How to use these aliases?

Copy the aliases in your file ~/.profile and in a terminal execute the following command:

Very short aliases

Docker toolbox installs many executables beginning by docker, so the combo “do” + TAB in the terminal is not efficient anymore. I use very short aliases, not amazing but useful 🙂

Start a docker machine

Every time you boot your iMac or your MacBook, you must start a docker machine, i.e. a virtual machine hosting the docker engine, by running the command:

I create a docker-machine-start function and I bind it to an alias. So executing:

will launch the default docker machine, create the environment variables for this host and create an extra environment variable DM_HOST which contains the IP address of the launched machine. This environment variable can be useful for referring to this IP address in your docker compose files, or to connect to the host via SSH for example.

Clean containers and images

When you build your own docker images, a lot of unused images can populate your local docker, this following alias removes these images:

And finally, this last alias removes all the created containers:

If you are using some aliases for docker too, you can share them with us!