-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fetching Entities
This document is being revised for MagicalRecord 2.3.0, and may contain information that is out of date. Please refer to the MagicalRecord's headers if anything here doesn't make sense.
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"];
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];
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.
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];
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 *)
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"];
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];
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_createEntityInContext:otherContext];
To delete a single entity:
[myPerson MR_deleteEntity];
or, to delete the entity from a specific context:
[myPerson MR_deleteEntityInContext: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];
MagicalRecord Guide
- Installing MagicalRecord
- Getting Started
- Working with Managed Object Contexts
- Creating Entities
- Deleting Entities
- Fetching Entities
- Saving Entities
- Usage Patterns
- Importing Data
- Logging
Upgrade Guides
Contributing to MagicalRecord
Resources