Skip to content

Instantly share code, notes, and snippets.

@rawrjustin
Created August 24, 2015 23:26
Show Gist options
  • Save rawrjustin/7331da16d6e637db20dc to your computer and use it in GitHub Desktop.
Save rawrjustin/7331da16d6e637db20dc to your computer and use it in GitHub Desktop.
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