如何使用缓存库添加JSON数据

时间:2019-04-21 06:24:00

标签: swift

我正在使用“ hyperoslo”的缓存库。(https://github.com/hyperoslo/Cache#handling-json-response

然后我尝试将Swift 3.x更新到4.2。 但是我在将Json数据添加到缓存时遇到了两个错误。

关于Cannot convert value of type 'Data' to expected argument type 'String'的错误消息之一。

func set(_ object: SwiftyJSON.JSON, for key: String) {
    guard let data = try? object.rawData() else { return }
    cache?.setObject(data, forKey: key)
}

另一个大约是Cannot convert value of type '()?' to closure result type 'Disposable'

我在此代码处遇到此错误。

self?.cache?.async.object(forKey: key) { result in

我不明白如何将JSON数据放入缓存。

我尝试将.add(key, object: data)的代码更改为.async.setObject。 但是我不明白如何使用setObject

这是所有用于缓存的代码。

import Foundation
import Cache
import SwiftyJSON
import RxSwift
import Async

final class CacheService {

    // MARK: - Properties
    static let shared = CacheService()

    fileprivate let cache = try? Storage(
        diskConfig: DiskConfig(name: "ReadGirl.JSONCache"),
        memoryConfig: MemoryConfig(expiry: .never, countLimit: 10, totalCostLimit: 10),
        transformer: TransformerFactory.forCodable(ofType: String.self) // Storage<User>
    )

    // MARK: - Enum Values
    enum Service: String {
        case book = "Readgirl.JSONCache.book"
        case thread = "Readgirl.JSONCache.thread"
        case sound = "Readgirl.JSONCache.sound"
    }

}

// MARK: - Save
extension CacheService {
    func set(_ object: SwiftyJSON.JSON, for service: Service) {
        set(object, for: CacheService.Service(rawValue: service.rawValue)!)
    }
    func set(_ object: SwiftyJSON.JSON, for key: String) {
        guard let data = try? object.rawData() else { return }
        cache?.setObject(data, forKey: key) <-- ERROR : Cannot convert value of type 'Data' to expected argument type 'String'
    }

}

// MARK: - Load
extension CacheService {
    func object(for service: Service) -> Observable<SwiftyJSON.JSON?> {
        return object(for: CacheService.Service(rawValue: service.rawValue)!)
    }

    func object(for key: String) -> Observable<SwiftyJSON.JSON?> {
        return Observable.create { [weak self] observer in
            //self?.cache?.async.object(forKey: key) { (data: Data?) in
            self?.cache?.async.object(forKey: key) { result in <-- ERROR : Cannot convert value of type '()?' to closure result type 'Disposable'
                switch result {
                    case .value(let data):
                        Async.userInteractive {
                            let json = SwiftyJSON.JSON(data: data)
                            Async.main {
                                if let error = json.error {
                                    observer.onError(error)
                                } else {
                                    observer.onNext(json)
                                }
                                observer.onCompleted()
                            }
                        }
                case .error(let error):
                    observer.onNext(nil)
                    observer.onCompleted()
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用setObject代替下面的add(key:

func set(_ object: SwiftyJSON.JSON, for key: String) {
    guard let data = try? object.rawData() else { return }
    cache?.setObject(data, forKey: key)
}

并使用Result<Type>storage检索所需的对象,如下所示,

self?.cache?.async.object(forKey: key) { result in
  switch result {
    case .value(let data):
         Async.userInteractive {
           let json = SwiftyJSON.JSON(data: data)

            Async.main {
                if let error = json.error {
                    observer.onError(error)
                } else {
                   observer.onNext(json)
                }
                observer.onCompleted()
            }
         }
    case .error(let error):
      observer.onNext(nil)
      observer.onCompleted()
  }
}