Swift 2.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 a JSON file / response, creates and stores those Events in a SQLite datastore. It is possible to do single and batch updates, deletions and retrieving, filtering on stored Events.
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.
The version used in this project is version 9.0
For this project the Apple Swift version 2.0
is used.
This project is build with Xcode version 7.0 beta
.
In this project the usage of pragmas will help you through the code exploration. For example:
Open up Xcode, and open the project, and open the PersistenceManager.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.
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.
The Event API is the interface where a view controller directly communicates to. The Event API exposes several methods to a View controller. The Persistency Managers implementation is completely hidden from the view controllers, thus therefore every request needs to go through the Event API. This intermediate step makes it a lot easier to maintain and extend the implementation of the Persistence Manager, since not every method implemented in the Persistence Manager will be needed to be exposed / accessible to externals. For this reason The Event API serves as a interface to do requests to. Every request to get Events/ Update Events / Delete Events will be done through the Event API which will eventually delegated to the Persistence Manager.
The Event API
can be seen as a library of available functions on Event retrieval, editing and storage.
The Persistence Manager is a manager that allows the actual CRUD
operations on the persistence store with an Event entity.
Currently it exposes the following functions:
Create
- Creates a new Managed object (Event) and persists to datastore.
Read
-
Retrieves all Event items, stored in the persistence layer.
-
Retrieve an Event, found by it's stored id.
-
Retrieves all Event items, stored in the persistence layer and sort it by Date.
Update
- Update Event attribute values
Delete
-
Delete all Event items stored, from persistence layer.
-
Delete a single Event item stored, from persistence layer.
The Persistency Manager
can be seen as the communicator to the persistent store coordinator.
More Core Data basics can be found here
A great tutorial on Design patterns can be found here. Note, that the pattern used in this project is inspired by these tutorials, with special remark on the facade design pattern.
-
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.