Skip to content

Latest commit

 

History

History
150 lines (98 loc) · 4.65 KB

Fetching.md

File metadata and controls

150 lines (98 loc) · 4.65 KB

Fetching

Basic Finding

Most methods in MagicalRecord return an NSArray of results.

Say you have an Entity called "Person", related to a Department (as seen in various Apple Core Data documentation). To get all of the Person entities from your Persistent Store, use the following method:

NSArray *people = [Person MR_findAll];

Or, to return the results sorted by a property:

NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName" ascending:YES];

Or, to return the results sorted by multiple properties:

NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName,firstName" ascending:YES];

Or, to return the results sorted by multiple properties with different attributes (these will default to whatever you set them to):

NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName:NO,firstName" ascending:YES];

// OR

NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName,firstName:YES" ascending:NO];

If you have a unique way of retrieving a single object from your data store (such as via an identifier), you can use the following method:

Person *person = [Person MR_findFirstByAttribute:@"firstName" withValue:@"Forrest"];

Advanced Finding

If you want to be more specific with your search, you can send in a predicate:

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", @[dept1, dept2]];
NSArray *people = [Person MR_findAllWithPredicate:peopleFilter];

Returning an NSFetchRequest

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", departments];
NSFetchRequest *people = [Person MR_requestAllWithPredicate:peopleFilter];

For each of these single line calls, the full stack of NSFetchRequest, NSSortDescriptors and a simple default error handling scheme (ie. logging to the console) is created.

Customizing the Request

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", departments];

NSFetchRequest *peopleRequest = [Person MR_requestAllWithPredicate:peopleFilter];
[peopleRequest setReturnsDistinctResults:NO];
[peopleRequest setReturnPropertiesNamed:@[@"firstName", @"lastName"]];

NSArray *people = [Person MR_executeFetchRequest:peopleRequest];

Find the number of entities

You can also perform a count of all entities of a specific type in your Persistent Store:

NSNumber *count = [Person MR_numberOfEntities];

Or, if you're looking for a count of entities based on a predicate or some filter:

NSNumber *count = [Person MR_numberOfEntitiesWithPredicate:...];

There are also complementary methods which return NSUInteger rather than NSNumber instances:

  • MR_countOfEntities
  • MR_countOfEntitiesWithContext:(NSManagedObjectContext *)context
  • MR_countOfEntitiesWithPredicate:(NSPredicate *)predicate
  • MR_countOfEntitiesWithPredicate:(NSPredicate *)predicatecontext inContext:(NSManagedObjectContext *)

Aggregate Operations

NSInteger totalFat = [[CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribute:@"fatCalories" withPredicate:predicate] integerValue];
NSInteger fattest  = [[CTFoodDiaryEntry MR_aggregateOperation:@"max:" onAttribute:@"fatCalories" withPredicate:predicate] integerValue];
NSArray *caloriesByMonth = [CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribute:@"fatCalories" withPredicate:predicate groupBy:@"month"];

Finding from a different context

All find, fetch, and request methods have an inContext: method parameter

NSArray *peopleFromAnotherContext = [Person MR_findAllInContext:someOtherContext];

Person *personFromContext = [Person MR_findFirstByAttribute:@"lastName" withValue:@"Gump" inContext:someOtherContext];

NSUInteger count = [Person MR_numberOfEntitiesWithContext:someOtherContext];

Creating new Entities

When you need to create a new instance of an Entity, use:

Person *myPerson = [Person MR_createEntity];

or, to specify which context the entity is inserted into:

Person *myPerson = [Person MR_createInContext:otherContext];

Deleting Entities

To delete a single entity:

[myPerson MR_deleteEntity];

or, to delete the entity from a specific context:

[myPerson MR_deleteInContext:otherContext];

There is no delete All Entities or truncate operation in core data, so one is provided for you with Active Record for Core Data:

[Person MR_truncateAll];

or, to truncate all entities in a specific context:

[Person MR_truncateAllInContext:otherContext];