NSFetchRequest setReturnsObjectsAsFaults: doesn't work
Asked Answered
F

1

8

I'm working on an application based on Core Data. I'm doing a fetch on Item. I don't want the fetched objects to be fault when returned because I'm sure they will be accessed immediately. Thus, I set the returnsObjectsAsFaults to be NO. However, it doesn't work for me.

Below is how I set up my NSFetchedResultsController.

NSString *cacheName = @"cache";
[NSFetchedResultsController deleteCacheWithName:cacheName];

NSManagedObjectContext *context = APPDELEGATE.managedObjectContext;

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:context];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateCreated" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[request setFetchBatchSize:20];
[request setReturnsObjectsAsFaults:NO];

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:cacheName];
[request release];

NSError *error = nil;
BOOL result = [frc performFetch:&error];
if(!result){
    NSLog(@"Unresolved core data error");
}

Any ideas?

Update: Actually, all the fetched objects are faults.

<_PFBatchFaultingArray 0x176f2bc0>(
<Item: 0x17613480> (entity: Item; id: 0x18ba3910 <x-coredata://5C2547EC-0420-4ED5-867D-087DEF4998EA/Item/p610> ; data: <fault>),
<Item: 0x18b832e0> (entity: Item; id: 0x176dda50 <x-coredata://5C2547EC-0420-4ED5-867D-087DEF4998EA/Item/p611> ; data: <fault>),
<Item: 0x18b97040> (entity: Item; id: 0x18b5f830 <x-coredata://5C2547EC-0420-4ED5-867D-087DEF4998EA/Item/p612> ; data: <fault>),
<Item: 0x18bd1d40> (entity: Item; id: 0x18bc2f30 <x-coredata://5C2547EC-0420-4ED5-867D-087DEF4998EA/Item/p613> ; data: <fault>),
<Item: 0x18b996c0> (entity: Item; id: 0x18b8f660 <x-coredata://5C2547EC-0420-4ED5-867D-087DEF4998EA/Item/p619> ; data: <fault>)
)
Freeboot answered 11/12, 2013 at 10:21 Comment(6)
explain us what do you mean with it does not work for meSpleenful
Do you have any logs/tests (CoreData debug logs) showing excessive faulting?Franciscka
@flexaddicted The fetched objects are still faults.Freeboot
@DanShelly What do u mean with "excessive faulting"? Thanks.Freeboot
@Libra, Do you see in the CoreData debug logs any faults that are not fulfilled in batches? The FRC is managing the faulting, try executing the request directly on the context and see if you get faults or fulfilled object.Franciscka
@DanShelly, Yes. actually, log shows fetched objects are all faults. I've edited the question and appended logs.Freeboot
L
1

I have observed this as well. In my case, I was fetching the results on a child context. When I changed the fetch to be on the main context, then returnsObjectsAsFaults was behaving properly.

Again, if I did the fetch from a child context, returnsObjectsAsFaults did not work as expected.

Someone was having a similar issue here, for the same reason: https://mcmap.net/q/1473828/-does-coredata-always-respect-returnobjectsasfaults

Liber answered 30/6, 2015 at 15:37 Comment(1)
Thank you so much! This solved my problem. I was fetching on a background context.Nephro

© 2022 - 2024 — McMap. All rights reserved.