/CoreData-CRUD-Swift-4.0-example

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

Primary LanguageSwiftMIT LicenseMIT

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

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

screenshotOverview

Prerequisites

Tested with iOS:

iOS9.3 & iOS10

iPhone

  • iPhone 6
  • iPhone 6s
  • iPhone 7

The objective

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

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.

The MIT License (MIT)

Copyright (c) 2016 - Jongens van Techniek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.