iPhone SDK: Core Data
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 ca开发者_Go百科n't seem to get anything working.
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
}
精彩评论