Prechádzať zdrojové kódy

Remove forced unwraps in Core Data Stack

Marvin Polscheit 4 dní pred
rodič
commit
e37167ee7d
1 zmenil súbory, kde vykonal 6 pridanie a 2 odobranie
  1. 6 2
      Model/CoreDataStack.swift

+ 6 - 2
Model/CoreDataStack.swift

@@ -336,7 +336,9 @@ extension CoreDataStack {
         taskContext.transactionAuthor = "batchDelete"
 
         // Get the number of days we want to keep the data
-        let targetDate = Calendar.current.date(byAdding: .day, value: -days, to: Date())!
+        guard let targetDate = Calendar.current.date(byAdding: .day, value: -days, to: Date()) else {
+            throw CoreDataError.validationError(function: callingFunction, file: callingClass)
+        }
 
         // Fetch all the objects that are older than the specified days
         let fetchRequest = NSFetchRequest<NSManagedObjectID>(entityName: String(describing: objectType))
@@ -394,7 +396,9 @@ extension CoreDataStack {
         taskContext.transactionAuthor = "batchDelete"
 
         // Get the target date
-        let targetDate = Calendar.current.date(byAdding: .day, value: -days, to: Date())!
+        guard let targetDate = Calendar.current.date(byAdding: .day, value: -days, to: Date()) else {
+            throw CoreDataError.validationError(function: callingFunction, file: callingClass)
+        }
 
         // Fetch Parent objects older than the target date
         let fetchParentRequest = NSFetchRequest<NSManagedObjectID>(entityName: String(describing: parentType))