Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

缓存机制报错 #4

Open
CoderDoraemon opened this issue Jul 1, 2019 · 3 comments
Open

缓存机制报错 #4

CoderDoraemon opened this issue Jul 1, 2019 · 3 comments

Comments

@CoderDoraemon
Copy link

/*
返回格式是以下,用对象去缓存正常
         {
         "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
Copy link
Owner

Pircate commented Jul 1, 2019

image

删了APP设置下缓存策略

@CoderDoraemon
Copy link
Author

CoderDoraemon commented Jul 1, 2019

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
Copy link
Owner

Pircate commented Jul 1, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants