Setting a property value on each of the results in a FetchedResults set

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-04-21T09:50:33Z Indexed on 2010/04/21 9:53 UTC
Read the original article Hit count: 283

Hi

On my Core Data Entity "Book" i have a boolean property, 'wasViewed' (NSNumber numberWithBool) that tells me if the Book was "viewed".

I would like to implement a sort of "reset" this property for all my NSManagedObjects "Book". So that I can set them all to NO between sessions. I use an NSPredicate to retrieve all the Books like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"wasViewed == %@", [NSNumber numberWithBool:YES]];

// code for setting entity, request etc...

NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];

This is working just fine, however, now I need to set up a loop, go through each Book object, something like this:

for(Book *b in mutableFetchResults) {

    [b setWasViewed:NO]
}

Is there a way to perform an action on each element that fits the predicate instead of retrieving it? So instead of executeFetchRequest on a managedObjectContext it could be executeOperationOnFetchRequestResults or something along those lines.

Thanks for any input given:)

© Stack Overflow or respective owner

Related posts about nsmanagedobjectcontext

Related posts about core-data