Git Product home page Git Product logo

coredatastack's Introduction

2016 onwards

NOTE: Apple's new NSPersistentContainer has been created to serve most of the purpose that CoreDataStack fulfilled; I strongly advise you try using the Apple class from here on instead of this one.

https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html

CoreDataStack

Simple classes to make Apple's Core Data usable for normal human beings...

...WITHOUT CHANGING the way CoreData works (i.e. this is not a "framework" - it's just a very thin wrapper to do the code that Xcode "New Project" template could have done in the first place.

Core Data is very easy to use - but Apple's source examples make it harder than it should be.

We fix: Xcode's bad examples / bad practices

  1. Xcode's default template for "a CoreData enabled app" puts 300 lines of INCORRECT, UNNECESSARY code into your Project.
  2. Xcode puts it all into AppDelegate - the worst possible place for this code.
  3. Apple's sample code forces you to have ONLY ONE CORE DATA MODEL open at a time. This is wrong, and throws away one of CoreData's awesome features: most CoreData projects would be many times easier to write and debug if they used multiple models. They would also run faster. But if you start with Apple's template, you cannot do this. CoreData was not designed to be used that way!

So, this simple library turns CoreData from:

 "300 lines of dense code in your AppDelegate class"

...into:

 "3 lines of simple, easy code you can re-use throughout your project"

License is:

Do whatever you want with this code. Attribution is appreciated - @t_machine_org on twitter - but not required.

Installation A: by drag/drop into your project

  1. Open the "source" folder
  2. Drag/drop all the files into your project

Installation B: by creating a static library

Not supported yet. Use "Installation A" instead (see above) If you really want it, add an item to the Issue Tracker on Github, and I'll make a library version.

Usage

To use CoreData, you ALWAYS need to have created a CoreData Model first. You also ALWAYS need to have defined a save location for the database.

Apple makes this difficult, but we make it easy again. All you need to know is the NAME of your CoreData Model (the graphical doc that lets you edit and add entities) - whatever name it appears in on the left hand side of Xcode. e.g. if it says "CoreDataModel.xcdatamodeld" then the "NAME" is "CoreDataModel"

Given the name, you do:

 CoreDataStack* stack = [CoreDataStack coreDataStackWithModelName:@"My_Model_Name"]];

Usage - Modern, improved versions of Apple methods

Most of the "common" actions you need to do with CoreData have strange method names, or exist on a different class from the one you were expecting.

Many of them take NSString arguments where a Class would be easier and more appropriate (NB: XCode autocomplete does NOT support Apple's strings - it only supports classes!)

So ... this also includes a bunch of methods that fix those two problems. Using them is entirely optional - they merely reduce the boilerplate code in your project, and make it typesafe.

e.g.:

  • insertInstanceOfClass: (creates a new NSManagedObject of the specified class)
  • fetchEntities:matchingPredicate: (gets all entities of the specified class that match a predicate)
  • countEntities:matchingPredicate: (counts the number of possible results)
  • storeContainsAtLeastOneEntityOfClass: (is this the first run of your app? Did the user wipe the CoreData store? Check this when starting your app...)
  • ...etc

Usage - Fallback to Apple basics

Then, everytime CoreData needs a "ManagedObjectContext" instance, you just pass it:

 stack.managedObjectContext

e.g.:

 NSEntityDescription* entityDesc = [NSEntityDescription entityForName:@"MyFirstEntity" inManagedObjectContext:stack.managedObjectContext];

...or, better, because the line above is BAD PRACTICE (even though Apple uses it in their source examples), use:

 NSEntityDescription* entityDesc = [stack entityForClass:[MyFirstEntity class]];

NB: if you use Apple's version, which takes an NSString, then refactoring in Xcode will NOT work! and any small typo will NOT be detected by the compiler - your app will instead crash at runtime. So ... don't pass in an NSString, pass in the class you want instantiated.

Easy!

coredatastack's People

Contributors

adamgit avatar azplanlos avatar couloirr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

coredatastack's Issues

Not passing context to ViewControllers

Hello Adam,

I stumbled upon this helper class you made and while I like the idea, I noticed you don't follow Apple convention of passing the context from the Application Delegate to the subsequent view controllers.

Instead, you seem to load everything up every time you want to operate with core data. I don't know if this is is more correct but I wanted your opinion on why you made it this way.

What I mean is, loading a managed object model from disk in every view controller, just seems like you will make the app spend to much time loading the model when you can easily pass a pointer to the ManagedObjectContext instance to your controllers.

The only reason I can see for doing this is having more than one model, but maybe I am not seeing something.

Well, thank you in advance for your reply.

Concurrency with Core Data

I am not sure when but it seems Apple supports concurrency programming with Core Data in two ways:
"There are two possible ways to adopt the pattern:

Create a separate managed object context for each thread and share a single persistent store coordinator.
This is the typically-recommended approach.

Create a separate managed object context and persistent store coordinator for each thread.
This approach provides for greater concurrency at the expense of greater complexity (particularly if you need to communicate changes between different contexts) and increased memory usage."

I know, it still requires us to carefully manage things.
Do you available for updating the "CoreDataStack" in stead of an assertion in code?

reference: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/Articles/cdConcurrency.html#//apple_ref/doc/uid/TP40003385-SW1

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.