Code Monkey home page Code Monkey logo

hitlist2's Introduction

HitList2

My next app will be in Swift, and it will use Core Data for object persistence. I recently learned Swift, and I needed to figure out Swiftian Core Data. Pietro Rea’s excellent tutorial “Your First Core Data App Using Swift” was an excellent start on this, but the code in the tutorial has two limitations.

First, the code uses KVC, rather than a NSManagedObjectContext subclass, to manipulate data. So, person.setValue(name, forKey: "name") rather than person.name = name. This approach is more verbose, and a typo in the key name would cause an runtime error.

Second, as in Apple’s Core Data boilerplate, the code for setting up the Core Data stack is all in AppDelegate.swift. This violates the single responsibility principle, which “states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class”. The responsibility of the class implementing UIApplicationDelegate is to respond "to important events in the lifetime of your app." Getting into the weeds of setting up the Core Data stack could not be further removed, in my view, from this responsibility. Rather, the one situation in which that class should interact with Core Data is to save the context when the application is about to terminate.

HitList2 addresses these limitations. First, the app uses a NSManagedObjectContext subclass to manipulate data. Second, the app moves the setup of the Core Data stack to its own singleton utility class, CDManager, but still saves the context when the app is about to terminate.

As an aside, CDManager uses Martin Rue's clever implementation of the Singleton pattern.

hitlist2's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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