PumpEventStored+CoreDataClass.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import CoreData
  2. import Foundation
  3. @objc(PumpEventStored) public class PumpEventStored: NSManagedObject {
  4. let errorDomain = "PumpEventStoredErrorDomain"
  5. enum PumpEventErrorType: Int {
  6. case duplicate = 1001
  7. }
  8. override public func awakeFromInsert() {
  9. id_ = UUID().uuidString
  10. }
  11. // override public func validateForInsert() throws {
  12. // try super.validateForInsert()
  13. // try validateUniqueTimestamp()
  14. // }
  15. //
  16. // private func validateUniqueTimestamp() throws {
  17. // guard let context = managedObjectContext, let timestamp = self.timestamp else {
  18. // return
  19. // }
  20. //
  21. // let fetchRequest: NSFetchRequest<PumpEventStored> = PumpEventStored.fetchRequest()
  22. // fetchRequest.predicate = NSPredicate.duplicateInLastFourLoops(timestamp)
  23. //
  24. // do {
  25. // let results = try context.fetch(fetchRequest)
  26. // if !results.isEmpty {
  27. // print("Found duplicate PumpEventStored objects:")
  28. // for result in results {
  29. // print("Timestamp: \(String(describing: result.timestamp))")
  30. // }
  31. // let error = NSError(domain: errorDomain, code: PumpEventErrorType.duplicate.rawValue, userInfo: [
  32. // NSLocalizedDescriptionKey: "There is already a PumpEventStored with the same timestamp within the last 20 minutes.",
  33. // "PumpEventErrorType": PumpEventErrorType.duplicate
  34. // ])
  35. // throw error
  36. // }
  37. // } catch {
  38. // throw error
  39. // }
  40. // }
  41. //
  42. // override public func validateValue(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey key: String) throws {
  43. // try super.validateValue(value, forKey: key)
  44. //
  45. // if key == "timestamp" {
  46. // try validateUniqueTimestamp(value)
  47. // }
  48. // }
  49. //
  50. // private func validateUniqueTimestamp(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>) throws {
  51. // guard
  52. // let timestamp = value.pointee as? Date
  53. // else {
  54. // return
  55. // }
  56. //
  57. // let fetchRequest: NSFetchRequest<PumpEventStored> = PumpEventStored.fetchRequest()
  58. // fetchRequest.predicate = NSPredicate.duplicateInLastFourLoops(timestamp)
  59. //
  60. // do {
  61. // let results = try CoreDataStack.shared.backgroundContext.fetch(fetchRequest)
  62. // if !results.isEmpty {
  63. // print("Found duplicate PumpEventStored objects:")
  64. // for result in results {
  65. // print("Timestamp: \(String(describing: result.timestamp))")
  66. // }
  67. // let error = NSError(domain: errorDomain, code: PumpEventErrorType.duplicate.rawValue, userInfo: [
  68. // NSLocalizedDescriptionKey: "There is already a PumpEventStored with the same timestamp within the last 4 loops.",
  69. // "PumpEventErrorType": PumpEventErrorType.duplicate
  70. // ])
  71. // throw error
  72. // }
  73. // } catch {
  74. // throw error
  75. // }
  76. // }
  77. }