2

I need to execute fetch request. But when I do it I get not fault NSManagedObjects (each of the objects is about 5 Mb, that's why I get the memory warning). Apple provides faulting possibility for Core Data (when objects are not loaded in RAM). And I wanna my objects to use this possibility.

Here is my code

+ (NSMutableSet *)GetImagesWithPredicate:(NSPredicate *)predicate
{
    NSString *entityName = kEntityName;
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];;
    NSManagedObjectContext *context = appDelegate.managedObjectContext;
    NSEntityDescription *entityDesctiption = [NSEntityDescription 
                                              entityForName: entityName
                                              inManagedObjectContext:context];

    // find object
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesctiption];
    [request setPredicate:predicate];
    NSArray *objects = [context executeFetchRequest:request error:nil];
    [request release];
    if (objects == nil)
    {
        NSLog(@"there was an error");
        return nil;
    }
    NSMutableSet *set = [NSMutableSet setWithArray:objects];
    return set;
}

where predicate is (id < 500).

App crashes after

NSArray *objects = [context executeFetchRequest:request error:nil];

because all the data of objects appears in the RAM of iPhone.

It seems that default option returnsObjectsAsFaults = YES doesn't work.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • Could you post the crash log? In addition say something about your model and images (size). – Lorenzo B Jun 06 '12 at 13:51
  • There was not crash log. Just memory warning and then it crashed. When I tried to fetch only 5 objects - everything is OK (set up fetchLimit) – Paul T. Jun 06 '12 at 13:55

2 Answers2

2

you could set the - (void)setResultType:(NSFetchRequestResultType)type for the NSFetchRequest and only get the relevant attributes for your Object with the -(void)setPropertiesToFetch:(NSArray *)values Method.

And only lazy loading the needed attributes.

Community
  • 1
  • 1
CarlJ
  • 9,461
  • 3
  • 33
  • 47
  • 1) what result type should I set? (I can't find) 2) about setPropertiesToFetch: I need to use all the properties later – Paul T. Jun 06 '12 at 13:48
  • I tried [request setResultType:NSDictionaryResultType]; but that didn't help – Paul T. Jun 06 '12 at 13:51
  • and you should set the setPropertiesToFetch and the Result type to : NSDictionaryResultType – CarlJ Jun 06 '12 at 14:26
  • I tried to set the setPropertiesToFetch and the Result type to : NSDictionaryResultType. That didn't help – Paul T. Jun 07 '12 at 06:26
  • I catched what you mean. I set in setPropertiesToFetch only id, but I can access all the properties. But I think, that Jesse Rusak is a bit better. – Paul T. Jun 07 '12 at 08:34
2

The objects are probably being returned as faults; you can verify this with isFault. The issue is that Core Data automatically pre-fetches the property values for those objects and places them in the row cache (in memory). You can disable this behaviour by setting includesPropertyValues to NO on the NSFetchRequest object.

See the includesPropertyValues documentation for details of all this and the performance implications.

As an aside, you might not want to store lots of large objects in the database directly. You probably should look into using external storage if you're targeting iOS 5, or else using separate files yourself with their names/paths/ids in Core Data.

Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • Thanks a lot. Setting includesPropertyValues to NO helped. – Paul T. Jun 07 '12 at 08:32
  • But now I have another issue. When I load the data, I wanna extract the id from each object, but when I want to get the id, it seem, that all the object appears in the RAM, and finally app crashes (when all objects appears in the RAM). Can I hide some fields of NSMAnagedObjects after they appear after I want to get another field? (except [context reset], because I need other objects) – Paul T. Jun 07 '12 at 09:33
  • If you're going to access a sub-set of properties of all the objects, you should use @meccan's suggestion and use an NSDictionaryResultType with setPropertiesToFetch: with your id property. – Jesse Rusak Jun 07 '12 at 23:21