tristanhimmelman/AlamofireObjectMapper

support cachedResponseForRequest

Closed this issue · 4 comments

please support mapping of Response get from NSURLCache

let cachedResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(request)

i support it

//How to use
let response: [Product] = NSURLCache.sharedURLCache().cachedResponseForRequest(request)!.ObjectMapperArraySerializer()

import Foundation
import ObjectMapper
extension NSCachedURLResponse {


    public  func ObjectMapperSerializer<T: Mappable>() -> T?{

        do {
            let JSON = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
            return Mapper<T>().map(JSON)!
        } catch {
            return nil
        }

    }


    public  func ObjectMapperArraySerializer<T: Mappable>() -> [T]{
        do {
            let JSON = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
            return Mapper<T>().mapArray(JSON)!
        } catch {
            return []
        }

    }
}

I do not plan on adding this functionality at the time. Thanks for the suggestion

@dimohamdy
Thank you, this solution works for me, since I had to manually cache response from alamofire(because server headers was set to no-cache etc.).