MagicalRecord does not return NSDictionary in a fetch request

Here is my code:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"aMostRecentFlag == 1"]; // find old records NSFetchRequest *fetchRequest = [PreferenceData MR_requestAllWithPredicate: filter]; [fetchRequest setResultType: NSDictionaryResultType]; NSDictionary *preferenceData = [PreferenceData MR_executeFetchRequest:fetchRequest]; 

I get a warning from the assembly:

 Incompatible pointer types initializing 'NSDictionary *' with an expression of type 'NSArray *' 

which indicates that MR is not returning an NSDictionary as documented; Is there something wrong with my code? And, by the way, nothing returns a query for a selection, although there is a record that should have been.

+6
source share
1 answer

It seems that the comments worked out the answer, but I'm not sure that everything is clear.

A query query returns an array of results. Typically, the objects representing the results will be the class specified in the model. If you subclass NSManagedObject (as it should be), it will be a subclass. If not, it will be a generic NSManagedObject whose properties you get through valueForKey:

However, you can request that the results be returned as dictionaries. This does not mean that the executeFetchRequest:error: method returns a dictionary. Rather, it means that every object in the array will be a dictionary.

In what format you get the results, you need to pull out the object (s) that you need from the array. A common idiom is simply to add .lastObject or .firstObject to the end of the executeFetchRequest:error: call. Sort of:

 MyObject *object = [context executeFetchRequest:request error:NULL].firstObject; 

A magic record is nothing more than a fancy wrapper around Core Data classes. It provides convenient methods, but all the usual semantics of Core Data are still preserved.

+4
source

All Articles