22
33#### Basic Finding
44
5- Most methods in MagicalRecord return an NSArray of results. So, if you have an Entity called Person, related to a Department (as seen in various Apple Core Data documentation), to get all the Person entities from your Persistent Store:
5+ Most methods in MagicalRecord return an ` NSArray ` of results.
66
7- //In order for this to work you need to add "#define MR_SHORTHAND" to your PCH file
8- NSArray *people = [Person findAll];
7+ 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:
98
10- // Otherwise you can use the longer, namespaced version
11- NSArray *people = [Person MR_findAll];
9+ ``` objective-c
10+ NSArray *people = [Person MR_findAll ];
11+ ```
12+
13+ Or, to return the results sorted by a property:
1214
13- Or, to have the results sorted by a property:
15+ ``` objective-c
16+ NSArray *peopleSorted = [Person MR_findAllSortedBy: @"LastName" ascending: YES ] ;
17+ ```
1418
15- NSArray *peopleSorted = [Person MR_findAllSortedBy:@"LastName" ascending:YES];
19+ Or, to return the results sorted by multiple properties:
1620
17- Or, to have the results sorted by multiple properties:
21+ ```objective-c
22+ NSArray *peopleSorted = [Person MR_findAllSortedBy:@"LastName,FirstName" ascending:YES];
23+ ```
1824
19- NSArray *peopleSorted = [Person MR_findAllSortedBy:@"LastName,FirstName" ascending:YES];
25+ Or, to return the results sorted by multiple properties with different attributes (these will default to whatever you set them to):
2026
21- Or, to have the results sorted by multiple properties with difference attributes - will default to whatever you set it to:
27+ ``` objective-c
28+ NSArray *peopleSorted = [Person MR_findAllSortedBy: @"LastName: NO ,FirstName" ascending: YES ] ;
2229
23- NSArray *peopleSorted = [Person MR_findAllSortedBy:@"LastName:NO,FirstName" ascending:YES];
30+ // OR
2431
25- NSArray *peopleSorted = [Person MR_findAllSortedBy:@"LastName,FirstName:YES" ascending:NO];
32+ NSArray * peopleSorted = [ Person MR_findAllSortedBy:@"LastName,FirstName: YES " ascending: NO ] ;
33+ ```
2634
27- If you have a unique way of retrieving a single object from your data store, you can get that object directly :
35+ 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 :
2836
29- Person *person = [Person MR_findFirstByAttribute:@"FirstName" withValue:@"Forrest"];
37+ ```objective-c
38+ Person *person = [Person MR_findFirstByAttribute:@"FirstName" withValue:@"Forrest"];
39+ ```
3040
3141#### Advanced Finding
3242
3343If you want to be more specific with your search, you can send in a predicate:
3444
35- NSArray *departments = [NSArray arrayWithObjects:dept1, dept2, ..., nil];
36- NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments ];
37-
38- NSArray *people = [Person MR_findAllWithPredicate:peopleFilter];
45+ ``` objective-c
46+ NSPredicate *peopleFilter = [NSPredicate predicateWithFormat: @" Department IN %@" , @ [ dept1, dept2 ] ] ;
47+ NSArray * people = [ Person MR_findAllWithPredicate : peopleFilter ] ;
48+ ```
3949
4050#### Returning an NSFetchRequest
4151
42- NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments];
43-
44- NSArray *people = [Person MR_findAllWithPredicate:peopleFilter];
52+ ```objective-c
53+ NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments];
54+ NSArray *people = [Person MR_findAllWithPredicate:peopleFilter];
55+ ```
4556
4657For 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.
4758
4859#### Customizing the Request
4960
50- NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments];
61+ ``` objective-c
62+ NSPredicate *peopleFilter = [NSPredicate predicateWithFormat: @" Department IN %@" , departments] ;
5163
52- NSFetchRequest *peopleRequest = [Person MR_requestAllWithPredicate:peopleFilter];
53- [peopleRequest setReturnsDistinctResults:NO];
54- [peopleRequest setReturnPropertiesNamed:[NSArray arrayWithObjects:@"FirstName", @"LastName", nil]];
55- ...
64+ NSFetchRequest * peopleRequest = [ Person MR_requestAllWithPredicate: peopleFilter ] ;
65+ [ peopleRequest setReturnsDistinctResults: NO ] ;
66+ [ peopleRequest setReturnPropertiesNamed:@[ @"FirstName", @"LastName"]] ;
5667
57- NSArray *people = [Person MR_executeFetchRequest:peopleRequest];
68+ NSArray * people = [ Person MR_executeFetchRequest: peopleRequest ] ;
69+ ```
5870
5971#### Find the number of entities
6072
61- You can also perform a count of entities in your Store, that will be performed on the Store
73+ You can also perform a count of all entities of a specific type in your Persistent Store:
6274
63- NSNumber *count = [Person MR_numberOfEntities];
75+ ```objective-c
76+ NSNumber *count = [Person MR_numberOfEntities];
77+ ```
6478
6579Or, if you're looking for a count of entities based on a predicate or some filter:
6680
67- NSNumber *count = [Person MR_numberOfEntitiesWithPredicate:...];
68-
69- There are also counterpart methods which return NSUInteger rather than NSNumbers:
81+ ``` objective-c
82+ NSNumber *count = [Person MR_numberOfEntitiesWithPredicate: ...] ;
83+ ```
84+
85+ There are also complementary methods which return `NSUInteger` rather than `NSNumber` instances:
7086
71- * countOfEntities
72- * countOfEntitiesWithContext :(NSManagedObjectContext * )
73- * countOfEntitiesWithPredicate :(NSPredicate * )
74- * countOfEntitiesWithPredicate :(NSPredicate * ) inContext:(NSManagedObjectContext * )
87+ * `MR_countOfEntities`
88+ * `MR_countOfEntitiesWithContext :(NSManagedObjectContext *)context`
89+ * `MR_countOfEntitiesWithPredicate :(NSPredicate *)predicate`
90+ * `MR_countOfEntitiesWithPredicate :(NSPredicate *)predicatecontext inContext:(NSManagedObjectContext *)`
7591
7692#### Aggregate Operations
7793
@@ -80,56 +96,55 @@ NSInteger totalFat = [[CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribut
8096NSInteger fattest = [[CTFoodDiaryEntry MR_aggregateOperation:@"max:" onAttribute:@"fatCalories" withPredicate:predicate] integerValue];
8197NSArray *caloriesByMonth = [CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribute:@"fatCalories" withPredicate:predicate groupBy:@"month"];
8298```
83-
99+
84100#### Finding from a different context
85101
86102All find, fetch, and request methods have an inContext: method parameter
87103
88- NSManagedObjectContext *someOtherContext = ...;
89-
90- NSArray *peopleFromAnotherContext = [Person MR_findAllInContext:someOtherContext];
91-
92- ...
93-
94- Person *personFromContext = [Person MR_findFirstByAttribute:@"lastName" withValue:@"Gump" inContext:someOtherContext];
95-
96- ...
104+ ``` objective-c
105+ NSArray *peopleFromAnotherContext = [Person MR_findAllInContext: someOtherContext] ;
97106
98- NSUInteger count = [Person MR_numberOfEntitiesWithContext :someOtherContext];
107+ Person * personFromContext = [ Person MR_findFirstByAttribute:@"lastName" withValue:@"Gump" inContext : someOtherContext ] ;
99108
109+ NSUInteger count = [ Person MR_numberOfEntitiesWithContext: someOtherContext ] ;
110+ ```
100111
101112## Creating new Entities
102113
103114When you need to create a new instance of an Entity, use:
104115
105- Person *myNewPersonInstance = [Person MR_createEntity];
106-
107- or, to specify a context:
108-
109- NSManagedObjectContext *otherContext = ...;
116+ ```objective-c
117+ Person *myPerson = [Person MR_createEntity];
118+ ```
110119
111- Person *myPerson = [Person MR_createInContext:otherContext];
120+ or, to specify which context the entity is inserted into:
112121
122+ ``` objective-c
123+ Person *myPerson = [Person MR_createInContext: otherContext] ;
124+ ```
113125
114126## Deleting Entities
115127
116128To delete a single entity:
117129
118- Person *p = ...;
119- [p MR_deleteEntity];
120-
121- or, to specify a context:
130+ ```objective-c
131+ [myPerson MR_deleteEntity];
132+ ```
122133
123- NSManagedObjectContext *otherContext = ...;
124- Person *deleteMe = ...;
134+ or, to delete the entity from a specific context:
125135
126- [deleteMe MR_deleteInContext:otherContext];
136+ ``` objective-c
137+ [myPerson MR_deleteInContext: otherContext] ;
138+ ```
127139
128140There is no delete *All Entities* or *truncate* operation in core data, so one is provided for you with Active Record for Core Data:
129141
130- [Person MR_truncateAll];
142+ ```objective-c
143+ [Person MR_truncateAll];
144+ ```
131145
132- or, with a specific context:
146+ or, to truncate all entities in a specific context:
133147
134- NSManagedObjectContext *otherContext = ...;
135- [Person MR_truncateAllInContext:otherContext];
148+ ``` objective-c
149+ [Person MR_truncateAllInContext: otherContext] ;
150+ ```
0 commit comments