/CoreData-CRUD-Swift-2.0-example

Swift 2.2 Example project that exposes the usage of Core Data to create Entities and to persist to a SQLite Datastore

Primary LanguageSwift

CoreData-CRUD-Swift-iOS-example Join the chat at https://gitter.im/srmds/CoreData-CRUD-Swift-iOS-example

Swift 2.2 - 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.

screenshotOverview

Prerequisites

Tested with iPhone 6.

The objective

for this project is 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.

Contributions

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.

Model

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.

Core Data API

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.

CoreData Thread confinement

Thread confinement

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

Event API

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 info

More Core Data basics can be found here

TODO

  • Make step by step guide from scratch to working prototype.