Browse Source

Added precondition safety check to cacheResponse API and added docstring

Christian Noon 7 years ago
parent
commit
7c29181521
1 changed files with 10 additions and 2 deletions
  1. 10 2
      Source/Request.swift

+ 10 - 2
Source/Request.swift

@@ -515,9 +515,17 @@ open class Request {
 
     // MARK: - Cached Responses
 
+    /// Sets the cached response handler for the `Request` which will be used when attempting to cache a response.
+    ///
+    /// - Parameter handler: The `CachedResponseHandler`.
+    /// - Returns:           The `Request`.
     @discardableResult
-    open func cacheResponse(with handler: CachedResponseHandler) -> Self {
-        protectedMutableState.write { $0.cachedResponseHandler = handler }
+    open func cacheResponse(using handler: CachedResponseHandler) -> Self {
+        protectedMutableState.write { mutableState in
+            precondition(mutableState.cachedResponseHandler == nil, "Cached response handler has already been set")
+            mutableState.cachedResponseHandler = handler
+        }
+
         return self
     }
 }