Skip to content

Wrappers around Notifications #23

@wnagrodzki

Description

@wnagrodzki
extension Notification {
    
    public struct ManagedObjectContextDidSave {
        
        public var insertedObjects: Set<NSManagedObject> { return notification.objects(for: NSInsertedObjectsKey) }
        public var updatedObjects: Set<NSManagedObject> { return notification.objects(for: NSUpdatedObjectsKey) }
        public var deletedObjects: Set<NSManagedObject> { return notification.objects(for: NSDeletedObjectsKey) }
        private let notification: Notification
        
        fileprivate init(notification: Notification) {
            precondition(notification.name == .NSManagedObjectContextDidSave)
            self.notification = notification
        }
    }
    
    public func asManagedObjectContextDidSave() -> ManagedObjectContextDidSave {
        return ManagedObjectContextDidSave(notification: self)
    }
}

extension Notification {
    fileprivate func objects(for key: String) -> Set<NSManagedObject> {
        return userInfo?[key] as! Set<NSManagedObject>? ?? []
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions