I've got it working. I had to return the managed objects that were on the main context.
By adding the code below to get the objects on the mainContext
this worked.
[fetchedObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id entity = [[[MDCDataStore sharedInstance] mainContext] objectWithID:[obj objectID]];
[objectsOnMainContxt addObject:entity];
}];
Is there a better way to do this?
The entire function is as below
+ (void)entityName:(NSString *)entityName predicate:(NSPredicate *)predicate relationshipKeyPathsForPrefetching:(NSArray *)relationshipKeyPathsForPrefetching success:(void (^)(NSArray *fetchedObjects))success failure:(void (^)(NSError *error))failure
{
NSManagedObjectContext* context = [[[MDCDataStore sharedInstance] store] newPrivateContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
[context performBlock:^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDescription];
[fetchRequest setPredicate:predicate];
[fetchRequest setRelationshipKeyPathsForPrefetching:relationshipKeyPathsForPrefetching];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
NSMutableArray *objectsOnMainContxt = [@[] mutableCopy];
if([fetchedObjects count] > 0)
{
[fetchedObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id entity = [[[MDCDataStore sharedInstance] mainContext] objectWithID:[obj objectID]];
[objectsOnMainContxt addObject:entity];
}];
if(success)
{
success(objectsOnMainContxt);
}
}
else if(failure)
{
NSError *error = [NSError errorWithCode:CoreDataErrorCodeEntityNotFound
reason: @"There were no managed objects that matched the predicate and that existed in the context."
description:[NSString stringWithFormat:@"No managed object matched the predicate '%@'.", [predicate predicateFormat]]
suggestion:@"Ensure you are passing in the correct params into the predicate."];
failure(error);
}
}];
}