GithubHelp home page GithubHelp logo

ching's Introduction

Ching

Ching is a budget app for iOS.

Tech Stack

Adding Core Data Entities

I found that I really like this approach to Core Data models and model-objects.

There is a BaseEntity class that inherits from NSManagedObject and from which all other entities should inherit. It has three base properties:

  • createdAt: the date that it was created required
  • updatedAt: last updated timestamp optional

So to add a new entity the following steps are required:

  1. Add the entity to the xcdatamodeld file
  2. In addition to any custom fields, add the two timestamps listed above. Uncheck "Optional" for createdAt
  3. Use the Xcode tool to generate the NSManagedObject subclass (only once) and make it inherit from BaseEntity, remove the base properties

Use the Xcode generator the first time to generate the subclasses but never again. I'm not using categories or mogenerator to extend the functionality of the classes. The article linked above puts it nicely:

You may wonder how to implement custom code in managed object subclasses if Xcode keeps overwriting these files when regenerating them. Well, the answer is pretty simple: don’t generate them with Xcode. If you think about it, the generated code in these classes is trivial and very easy to write yourself, or generate once and then keep up to date manually. It’s really just a bunch of property declarations.

There are other solutions like putting the custom code in a category, or using tools like mogenerator, which creates a base class for each entity and a subclass of it where the user-written code is supposed to go. But none of these solutions allow for a flexible class hierarchy independent of the entity hierarchy. So at the cost of writing a few lines of trivial code, our advice is to just write those classes manually.

Testing Core Data Entities

In order to test methods on the BaseEntity class I added it as an entity to the xcdatamodel which is kind of smelly because I should never insert BaseEntity objects into the database but I needed a way to unit test the base class.

ching's People

Contributors

armstrongnate avatar

Watchers

 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.