views:

52

answers:

1

Probably a silly question but I cannot find a way to do it.

I am developing an iPhone application that uses Core Data for it's storage. At one point I want to loop around all the objects in the store and perform and action on them. Is there an easy way to do this? I've tried all manner of for and while loops but can't seem to get anything working.

Thanks.

+2  A: 

If you perform a fetch request on your managed object context it returns an array that you can then loop through.

NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"SomeEntity"
                               inManagedObjectContext:context]];
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:request error:&error];

if (error) {
    // an error occured
}

for (SomeEntity * object in objects) {
    // perform action
}
Cory Kilger
Perfect. Knew it would be a simple one. Thanks for the help.
gnuchu