You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added date encoding support for clients that may need to use a different formatting than ISO. (#53)
* Added the ability to customize encoding and decoding dates when parking or retrieving Codable objects.
* Split out SwiftCodableTests into separate files to significantly reduce file size.
Copy file name to clipboardExpand all lines: Sources/GarageStorage/Garage.swift
+59-1Lines changed: 59 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -33,10 +33,22 @@ public class Garage: NSObject {
33
33
34
34
/// Autosave is set to true by default, for every operation that causes a change to the underlying Core Data Managed Object Context.
35
35
///
36
-
/// When set to true, the garage will be saved after any operation that causes a change to the underlying Core Data Managed Object Context, including `park()`, `setSyncStatus()`, and `delete()`. When set to false, `save()` must be called instead, in order to persist those changes. You might want to set this to falseto perform batch changes to many objects before saving them all, to optimize performance.
36
+
/// When set to true, the garage will be saved after any operation that causes a change to the underlying Core Data Managed Object Context, including `park()`, `setSyncStatus()`, and `delete()`. When set to false, `save()` must be called instead, in order to persist those changes. You might want to use `withAutosaveDisabled(:)` to set this to false, in order to perform batch changes to many objects before saving them all, optimizing performance.
37
37
@objc(autosaveEnabled)
38
38
publicvarisAutosaveEnabled=true
39
39
40
+
/// The date decoding strategy used by JSONDecoder when decoding objects.
41
+
///
42
+
/// By default, this is set to a custom strategy that uses an internal `decodeTransformableDate` function that uses `Date.isoFormatter `for Codable objects or a wrapper conversion check for backward compatibility with Objective-C `MappableObject`.
43
+
/// You can modify this to use a different date decoding strategy by wrapping a `retrieve()` call `withDateDecodingStrategy() { }`.
/// The domain for errors generated by GarageStorage.
41
53
@objc
42
54
publicstaticleterrorDomain="GSErrorDomain"
@@ -157,6 +169,52 @@ public class Garage: NSObject {
157
169
}
158
170
returntryclosure()
159
171
}
172
+
173
+
/// Temporarily changes the date decoding strategy while executing the provided closure, then restores the previous strategy.
174
+
///
175
+
/// This is useful when you need to retrieve objects that were encoded with a different date strategy, or when you need to temporarily use a different decoding strategy. The previous strategy is automatically restored even if the closure throws an error.
176
+
///
177
+
/// - parameter strategy: The temporary date decoding strategy to use.
178
+
/// - parameter closure: A closure to execute with the temporary date decoding strategy. If the closure throws, the error is propagated after restoring the previous strategy.
179
+
/// - throws: Rethrows any error thrown by the closure.
180
+
///
181
+
/// Example usage:
182
+
/// ```swift
183
+
/// let person = try garage.withDateDecodingStrategy(.iso8601) {
/// Temporarily changes the date encoding strategy while executing the provided closure, then restores the previous strategy.
197
+
///
198
+
/// This is useful when you need to park objects with a different date encoding strategy, or when you need to temporarily use a different encoding strategy. The previous strategy is automatically restored even if the closure throws an error.
199
+
///
200
+
/// - parameter strategy: The temporary date encoding strategy to use.
201
+
/// - parameter closure: A closure to execute with the temporary date encoding strategy. If the closure throws, the error is propagated after restoring the previous strategy.
202
+
/// - throws: Rethrows any error thrown by the closure.
0 commit comments