RileyDev
RileyDev

Reputation: 2525

How to delete Managed Objects associated with another Managed Object

I may have not asked correctly but basically I have two Managed Object Subclass one called Folder another for Items to create different shopping lists. Each Folder has many Items and each Item has one Folder.

The problem I am having is when I delete a Folder the Items associated with it are not also deleted there still hang around in the persistent store.

Does anybody know how I can I also delete the Items that have a relationship with the Folder upon deleting the Folder ?

Folder:

class Folder: NSManagedObject {

@NSManaged var arrayOfItems: [Items]
@NSManaged var date: NSDate
@NSManaged var title: String
@NSManaged var Items: NSSet 

} 

Items

class Items: NSManagedObject {

@NSManaged var date: NSDate
@NSManaged var index: NSNumber
@NSManaged var item: String
@NSManaged var folder: Folder

}

Deleting a folder:

context.deleteObject(self.selectedNotebook)

// Save the context.
  do {
       try context.save()
     } catch {
       print("error saving")
     }

Upvotes: 1

Views: 129

Answers (1)

Leonardo
Leonardo

Reputation: 1750

Go to your data model file, select the relationship, and on the right side panel select the Delete Rule Cascade

Also, this page can help you understand core data better

Upvotes: 4

Related Questions