Guest User

Untitled

a guest
Mar 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. /// Something that can be created in a core data context, usually an `NSManagedObject`.
  2. public protocol CDCreatable {
  3. /// Creates this object in the context given.
  4. ///
  5. /// - Parameter context: Managed object context.
  6. /// - Remarks: Default constructor generated by `NSManagedObject`.
  7. /// - Warning: When creating objects, use `init(createIn:)` instead as it may contain additional initilization logic.
  8. init(context: NSManagedObjectContext)
  9.  
  10. /// Creates this object in the context given.
  11. ///
  12. /// - Parameter context: Managed object context.
  13. init(createIn context: NSManagedObjectContext)
  14. }
  15.  
  16. // MARK: - Default Implementation
  17.  
  18. extension CDCreatable where Self: CDCreatable {
  19. public init(createIn context: NSManagedObjectContext) {
  20. self.init(context: context)
  21. }
  22. }
  23.  
  24. // MARK: - Utilities
  25.  
  26. extension CDCreatable where Self: NSManagedObject {
  27. /// Specifies that this object should be removed from its persistent store when changes are committed. When changes are
  28. /// committed, the object will be removed from the uniquing tables. If object has not yet been saved to a persistent store,
  29. /// it is simply removed from `context`.
  30. ///
  31. /// - Parameter context: Managed object context.
  32. func delete(in context: NSManagedObjectContext) {
  33. context.delete(self)
  34. }
  35. }
Add Comment
Please, Sign In to add comment