-
-
Save rawrjustin/7331da16d6e637db20dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelCacheBridge: NSObject, ResponseBridge { | |
var modelCache: ZUM_ModelCache | |
init(modelCache: ZUM_ModelCache) { | |
self.modelCache = modelCache | |
super.init() | |
} | |
func process<ReturnType>(endpoint: Endpoint<ReturnType>, response: NSHTTPURLResponse?, responseObject: ResponseObject) -> ProcessResults { | |
do { | |
let serializedObject = try ReturnType.parseResponseObject(responseObject.rawValue()) as! ReturnType | |
let serializedArray = serializedObject as? NSArray | |
var canCache = false | |
if let sArray = serializedArray { | |
canCache = sArray.count > 0 && sArray[0].conformsToProtocol(ZUM_CacheableModelProtocol) | |
} else if let responseObj = serializedObject as? ZUM_JSONModel { | |
canCache = responseObj.conformsToProtocol(ZUM_CacheableModelProtocol) | |
} | |
if (canCache) { | |
if let sArray = serializedArray { | |
for obj in sArray { | |
if let jsonObj = obj as? ZUM_JSONModel { | |
self.modelCache.cacheModel(jsonObj, cacheOptions: options) | |
} | |
} | |
} else if let responseObj = serializedObject as? ZUM_JSONModel { | |
self.modelCache.cacheModel(responseObj, cacheOptions: options) | |
} | |
} | |
} catch { | |
// Parsing error during model cache parsing, stop and return error results | |
return ProcessResults(true, BridgeErrorType.Parsing) | |
} | |
return ProcessResults(true, nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment