WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How do I use Core Data across an application?

Forums > Swift

First of all, big thanks to this site for existing, as it's arguably the single best site for learning Swift.

The Core Data project, Project 38, is a good start, but...

I have been tasked with developing an application that receives images, saves them to disk, and then uploads them to a server. The requirements for the application are that images should exist on disk locally even after being uploaded, the network should not be considered 100% reliable, and that the upload should not be a blocking operation. Everything makes sense so far, right?

Obviously, Core Data is involved. I need to store where the images are saved as well as various bits of metadata about them, including whether they have been uploaded to the server. So part of the application saves the record of the files to Core Data, and another part will read them, upload them, and then store that they've been uploaded. Yet another part of the application should allow the user to search through past (saved) files and access them.

How should I make the different pieces of the application use Core Data? Should (can) I use a singleton class from each part/thread? Can I instantiate different contexts for each piece (or will they overwrite each other)? Should I create an instance of NSPersistentController on the ViewController and then pass it around to all the code that will use it?


This cant be answered in a single reply becuase it is an advanced topic. I would suggest looking at the different core data examples on here, the search is good for that. Also the process in SwiftUI is different from the process in UIKit, which one are you intending to use.


I'm using UIKit, as it's a Mac application using code not available in SwiftUI.

I'm not necessarily looking for a complete answer, as long as I can get pointed in the right direction.

For example, is this what I'm looking for? https://www.raywenderlich.com/7586-multiple-managed-object-contexts-with-core-data-tutorial


I think you should stick with just the single context and access it thorough the app. Although if you want to do some heavy Core Data tasks, you should use background context for this to not freeze the UI.

As was already mentioned, this is quite involved topic. If you want, you can check out my article about Core Data setup. It is for iOS app but I think it should work identically for macOS.


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.