mac-cain13 / CoreDataKit

CoreDataKit makes common operations on objects and importing into CoreData a breeze.
MIT License
35 stars 17 forks source link

Support willSave on newly inserted objects #9

Open mac-cain13 opened 9 years ago

mac-cain13 commented 9 years ago

CoreData doesn't call willSave for newly inserted objects. So many of my projects include this "fix", maybe CoreDataKit could include this as a optional fix that you can enable?!

// Apply "fix" so willSave is also called for inserted objects
NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextWillSaveNotification, object: nil, queue: NSOperationQueue.mainQueue()) { notification in
      if let managedObjectContext = notification.object as? NSManagedObjectContext {
        for managedObject in managedObjectContext.insertedObjects {
          managedObject.willSave()
        }
      }
    }