Swift 3.0 - A (very simple) example project that exposes the usage of CoreData to create entities and to persist to a SQLite Datastore.
This app demonstrates Core Data and persistent storage, by reading Event data from both, locally and remotely retrieved JSON file / response, creates and stores those Events in a SQLite datastore. It is possible to do single and batch updates, deletions, retrieving and filtering on stored Events.
Tested with iPhone 6.
for this project is to learn (for now mostly myself ;D ) to:
-
use Core Data to create Entities and to persist Entities to a SQLite datastore.
-
help others understand and use Core Data with simple, yet concrete examples, on the usage of Core Data and persistent store.
Note that this example project is non-exhaustive and since i'm still learning (more) about the Core Data framework, any progress on my part will be reflected in this project as updates to it.
Do you have questions or want to help? Enhancements and/or fixes and suggestions are welcome! Just drop a line via gitter of create issues and/or pull requests.
A model represents the entity that can be used to store in the datastore. The Event Entity/ Model has the following model attributes:
class Event: NSManagedObject {
@NSManaged var title: String
@NSManaged var date: NSDate
@NSManaged var venue: String
@NSManaged var city: String
@NSManaged var country: String
@NSManaged var attendees: AnyObject
@NSManaged var fb_url: AnyObject
@NSManaged var ticket_url: AnyObject
@NSManaged var eventId: String
}
The AnyObject type in this example are non-standard persistent attributes that are not support directly in Core Data. The AnyObject, as the name suggests, can therefore be for example an: Array
or NSURL
, or any other object type.
This application utilizes the Core Data stack concurrently to locally persist data. Below an overview of how the Core Data stack is implemented and utilized within the application.
You can see that there are three layers used, this is to provide true concurrency and also utilize thread confinement.
The minions* workers
are the workers in the EventAPI
that save each parsed
and prepared NSManagedObject
within it's own Thread. Eventually when all NSManagedObjects are stored within the thread confined context, the EventAPI
calls the MainContext
via the PersistenceManager
, which in turn will call ContextManager
and cause the minions
to merge / synchronize with the MainContext and finally with the Master application context
, which finally calls the DataStore Coordinator
to actually store the NSManagedObjects to the datastore.
More info on concurrency
The Event API is the interface where a view controller directly communicates to. The Event API exposes several endpoints to a View controller to create, read, update, delete Events.
Open up Xcode, and open the project, and open the EventAPI.swift
file.
Then click on ^6
, thus control + 6
, this will open up an overview of several CRUD methods used, and click on the method of interest, to see it's implementation.
*No copyright infringement intended.
More Core Data basics can be found here
-
Get a better understanding of relationships, no pun intended, both in real-life and in Core Data.
-
Make step by step guide from scratch to working prototype.