Search code examples
swiftcore-datasql-delete

CoreData removeFrom vs delete


In CoreData I have a one to many relationship between Quote and Image. (One quote can have many images.) I've been using quote.removeFromImage(image) when I want to delete an image, and even though it no longer appears in my app, I've noticed that it's still in my persistent store. I believe if I use delete it will be gone for good, but perhaps I'm using removeFrom incorrectly?

func deleteImage(imageIndex: Int) {
   let quote = currentQuote //currentQuote was fetched with the context
   let image = currentImageArray[imageIndex]
   quote?.removeFromImages(image)
   coreData.saveContext()
 }

Solution

  • removeFromImages only deletes the relation between the Quote and the Image objects but both remains in your store. If you delete an image and it has a relation with a quote then that relation will automatically be deleted.