views:

177

answers:

2

I don't think the class NSDeleteRequest exists, but I want it to.

I can make an NSPredicate and use NSFetchRequest to issue:

select * from foo where x=y

How can I issue:

delete from foo where x=y

?

The only way to delete 1000's of rows seems to be to fetch them, loop thru them, and call delete on each NSManagedObject. Is that right? Why can't I just issue 1 delete sql statement?

+4  A: 

You must fetch the managed objects and then send -deleteObject: to the context for each object you want to delete.

The objects must be fetched into memory so that Core Data can honor the cascade or nullify rule specified on the relationship.

If you are having a specific performance problem with delete (beyond the required fetch) it would be helpful to profile the operation in Instruments. If you are individually firing faults as the cascade/nullify rules are being applied, prefetching related objects will improve performance.

Jim Correia
A: 

You could add a category method -(executeDeleteWithRequest:error:) to NSManagedObjectContext.

Sure you would still need to do the looping but at least it abstracts the problem away and DRYs up your code.