Pircate / RxNetwork

A swift network library based on Moya/RxSwift.
MIT License
50 stars 4 forks source link

缓存机制报错 #4

Open CoderDoraemon opened 5 years ago

CoderDoraemon commented 5 years ago
/*
返回格式是以下,用对象去缓存正常
         {
         "top_stories": []
         }
         */  

Demo中下面此示例方法在解析过程中报错

/*
返回格式是以下,用数组对象去缓存报错
  {
   "code": 2000,
   "message": "Ok",
   "result": []
   }
*/
BannerAPI.test(count: 10)
      .onCache([BannerModel].self, { (models) in
          debugPrint("onCache:", models.first?.name ?? "")
      })
      .requestObject()
      .subscribe(onSuccess: { (models) in
          debugPrint("onSuccess:", models.first?.name ?? "")
      }, onError: { (error) in
          debugPrint("error:", error)
      })
     .disposed(by: disposeBag)

报错如下

Moya_Logger: [01/07/2019 17:56:24] Request: https://app01.chengtay.com:82/m/banner
Moya_Logger: [01/07/2019 17:56:24] Request Headers: ["Content-Type": "application/json"]
Moya_Logger: [01/07/2019 17:56:24] HTTP Request Method: POST
Moya_Logger: [01/07/2019 17:56:24] Request Body: {"sign":"","body":{"count":10},"token":""}
Moya_Logger: [01/07/2019 17:56:24] Response: <NSHTTPURLResponse: 0x600002f64140> { URL: https://app01.chengtay.com:82/m/banner } { Status Code: 200, Headers {
    "Cache-Control" =     (
        "no-cache",
        "no-store"
    );
    Connection =     (
        "keep-alive"
    );
    "Content-Type" =     (
        "application/json;charset=UTF-8"
    );
    Date =     (
        "Mon, 01 Jul 2019 09:56:24 GMT"
    );
    Expires =     (
        "Thu, 01 Jan 1970 00:00:00 GMT"
    );
    Pragma =     (
        "no-cache"
    );
    Server =     (
        "nginx/1.10.2"
    );
    "Set-Cookie" =     (
        "token=8621f0a4-48d0-4348-8fd3-eec3207ee863;Path=/; HttpOnly"
    );
    "Transfer-Encoding" =     (
        Identity
    );
} }
{
  "code" : 2000,
  "result" : [
    {
      "status" : 1,
      "img" : "banner\/20180920\/bb734a7e95f14a93a0edb8acbc572ad5.jpg",
      "content" : "https:\/\/www.chengtay.com\/",
      "id" : 11,
      "isDeleted" : 1,
      "createTime" : "2018-09-20 09:42:59",
      "updateTime" : "2018-11-01 14:29:24",
      "type" : 2,
      "description" : "https:\/\/www.chengtay.com\/",
      "name" : "公司",
      "pos" : null
    }
  ],
  "message" : "OK",
  "token" : "8621f0a4-48d0-4348-8fd3-eec3207ee863"
}
"error:" Moya.MoyaError.objectMapping(Swift.DecodingError.typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "result", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "id", intValue: nil)], debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil)), Status Code: 200, Data Length: 368)
Pircate commented 5 years ago

image

删了APP设置下缓存策略

CoderDoraemon commented 5 years ago

OnCache+Demo.swift requestObject 方法指定了 CleanJSONDecoder 缓存正常

import RxSwift
import RxNetwork
import CleanJSON

extension OnCache {

    public func requestObject() -> Single<C> {
        return target.request()
            .storeCachedResponse(for: target)
            .map(Network.Response<C>.self, using: CleanJSONDecoder())
            .map {
                if $0.success {
                    return $0.data
                }
                throw Network.Error.status(code: $0.code, message: $0.message)
            }
    }
}

onCache获取缓存需指定解析对应路径

/*
         {
         "code": 2000,
         "message": "Ok",
         "result": []
         }
         */
        BannerAPI.test(count: 10)
            .onCache([BannerModel].self, atKeyPath: "result", using: CleanJSONDecoder(), { (models) in
                debugPrint("onCache:", models.first?.name ?? "")
            })
            .requestObject()
            .subscribe(onSuccess: { (models) in
                debugPrint("onSuccess:", models.first?.name ?? "")
            }, onError: { (error) in
                debugPrint("error:", error)
            })
            .disposed(by: disposeBag)
Pircate commented 5 years ago

这个是正常的,JSON 解析失败导致的,Demo 里面我没有处理这个,多谢反馈。