Code Monkey home page Code Monkey logo

coredata-crud-swift-2.0-example's Introduction

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

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.

screenshotOverview

The objective

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.

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.

Versions

iOS

The version used in this project is version 9.0

Swift

For this project the Apple Swift version 2.0 is used.

Xcode

This project is build with Xcode version 7.0 beta.

Overview /utilities

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.

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.

Event API & Persistence Manager

Event API

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.

Persistence Manager

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 info

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.

TODO

  • 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.

coredata-crud-swift-2.0-example's People

Contributors

gitter-badger avatar srmds avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.