Giter VIP home page Giter VIP logo

timanagedobjectextensions's Introduction

#TIManagedObjectExtensions *A category on NSManagedObject to facilitate easy creation, fetching and counting of objects with Core Data *

Tim Isted
http://www.timisted.net
Twitter: @timisted

##License TIManagedObjectExtensions is offered under the MIT license.

##Summary TIManagedObjectExtensions is a category on NSManagedObject, adding one-line code utilities to subclasses of NSManagedObject, making it easy to:

  • Create an instance in a given managed object context (MOC).
  • Create a fetch request for a given MOC with optional predicate and/or sort descriptor(s).
  • Fetch all instances of an entity in a MOC with optional sort descriptor(s).
  • Fetch instances matching a predicate, or predicate format string and arguments.
  • New: Fetch a single instance matching a predicate.
  • Count all instances of an entity in a MOC.
  • Count instances matching a predicate, or predicate format string and arguments.

All methods are class methods, prefixed with ti_ to avoid future conflicts.

##Basic Usage Copy all the files in the TIManagedObjectExtensions directory into your project.

TIManagedObjectExtensions makes use of a method called ti_entityName to get the name of the entity for this class. It takes the name of the class and removes the first two characters, to convert e.g. a classname of TIEmployee to an entity name of Employee. If your class and entity don't follow this naming convention, you'll need to override ti_entityName to return a string with the name of your entity.

TIManagedObjectExtensions currently only works for subclasses of NSManagedObject, not NSManagedObject itself, because it obviously can't determine the name of the entity (in a class method) for generic NSManagedObject. In the future, all methods will have ti_someMethod:::forEntityName: equivalents.

The methods in TIManagedObjectExtensions.h should all be self explanatory.

###Creating Objects To create a new managed object for the current entity: TIEmployee *newEmployee = [TIEmployee ti_objectInManagedObjectContext:someContext];

This method returns a new managed object for the entity, inserted into the managed object context.

(See TICoreDataUtilties for easy creation of managed object contexts without using template code)

###Fetching Objects ####All Objects To fetch all the objects for the current entity: NSError *anyError = nil; NSArray *results = [TIEmployee ti_allObjectsInManagedObjectContext:someContext error:&anyError];

####Matching Predicates To fetch objects matching a predicate, you can either create the predicate: NSPredicate *testPredicate = [NSPredicate predicateWithFormat:@"self.department == %@", someDepartment];

results = [TIEmployee ti_objectsMatchingPredicate:testPredicate
                           inManagedObjectContext:someContext
                                            error:&anyError];

or specify it as a format string:
results = [TIEmployee ti_objectsInManagedObjectContext:someContext error:&anyError matchingPredicateWithFormat:@"self.department == %@", someDepartment];

####Sorting All relevant methods can also take either a single sort descriptor, or an array of sort descriptors: NSSortDescriptor *singleDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES]

results = [TIEmployee ti_objectsMatchingPredicate:testPredicate
                           inManagedObjectContext:someContext
                             sortedWithDescriptor:singleDescriptor
                                            error:&anyError];

You can also specify the name of a key for sorting and whether the sort should be ascending or descending: results = [TIEmployee ti_allObjectsInManagedObjectContext:someContext sortedByKey:@"dateOfBirth" ascending:YES error:&anyError];

####Fetching a Single Object To return only the first object that matches a predicate, you can use one of the ti_firstObjectIn... methods: TIEmployee *anEmployee = [TIEmployee ti_firstObjectInManagedObjectContext:someContext error:nil matchingPredicateWithFormat:@"self.employeeID matches %@", someID];

If no objects match, this will return nil.

##To Do List

  • Add non-entity-specific versions of all methods

timanagedobjectextensions's People

Contributors

timisted avatar

Stargazers

ppm avatar Mark Kirk avatar  avatar  avatar Jim Kubicek avatar Tom Mulder avatar Ching-Lan HUANG avatar Willi Wu avatar Marcus S. Zarra avatar

Watchers

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