In the following code, I explicitly set returnObjectsasFaults as false. Then RIGHT after the request I check to see if objects are fault or not. NSAssert fail.
Perhaps it's because the object is an imageBlob. Perhaps I am missing something? I just want to make sure.
This is a minor issue. If I get rid the nsassert, then my programs will run anyway. Still it sucks.
+(NSFetchRequest * )fetchRequestInContext:(NSString*) entityName:(NSPredicate *) predicate:(NSString*) sortKey:(BOOL) sortAscending
{
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[BGMDCRManagedObjectContextThreadHandler managedObjectContext]];
[request setEntity:entity];
if(predicate != nil)
{
[request setPredicate:predicate];
}
if(sortKey != nil)
{
NSMutableArray * sortDescriptorArray =[self getMoreSearchDescriptorsForEntity:entityName];
[request setSortDescriptors:sortDescriptorArray];
}
//request.fetchLimit = 200; //can be overridden somewhere else
request.returnsObjectsAsFaults = false;
if (entityName == BusinessString)
{
request.relationshipKeyPathsForPrefetching = arrayRelationship;
}
//[request setIncludesSubentities:<#(BOOL)#>
return request;
}
+(NSArray *) searchObjectsInContextEntityName:(NSString*) entityName Predicate:(NSPredicate *) predicate SortKEy:(NSString*) sortKey Booelan:(BOOL) sortAscending
{
NSManagedObjectContext * moc =[BGMDCRManagedObjectContextThreadHandler managedObjectContext];
NSFetchRequest *request = [self fetchRequestInContext:entityName:predicate:sortKey:sortAscending];
NSError *error;
if (entityName==BusinessString)
{
error=nil; //Some code for breakpoint
}
NSArray *fetchedObjects = [moc executeFetchRequest:request error:&error];
for (NSManagedObject * mo in fetchedObjects) {
NSAssert(!mo.isFault, @"For some reason mo is fault");
}
return fetchedObjects;
}
I've encountered the same problem while operating on a child NSManagedObjectContext. I create one as follows
NSManagedObjectContext *workerMOC = nil;
workerMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
workerMOC.parentContext = self.moc; // this is my main NSManagedObjectContext
Now after that if I do:
[workerMOC performBlock:^{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Company"];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSArray *allCompanies = [workerMOC executeFetchRequest:fetchRequest error:nil];
}];
I get faults in allCompanies. Which of course, just to clarify, does not happen if I execute the fetch request on self.moc.
However, I get appropriate pre-fetched results if I use the approach below:
[workerMOC performBlock:^{
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:workerMOC];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSArray *allCompanies = [workerMOC.persistentStoreCoordinator executeRequest:fetchRequest
withContext:workerMOC
error:nil];
}];
So it seems to be the case, that fetching on NSManagedObjectContexts tied directly to the NSPersistentStoreCoordinator works just fine. But in case of child NSManagedObjectContexts, which are not tied directly to the store, but rather to the parent context, do not behave as expected. I could not find anything related to this matter in the Apple docs, but still, I don't think it's a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With