Getting and setting JSON data in cache with Haneke Swift & SwiftyJSON

640 Views Asked by At

I'm currently struggling with trying to cache data and retrieve data which is being parsed using SwiftyJSON.

For the first method I'm trying to cache the data using the following function

Cache function:

func cacheStory(data: SwiftyJSON.JSON){

    let cache = Shared.JSONCache

    if let dataToCache: Haneke.JSON  = data as? Haneke.JSON {

       // print(dataToCache)
        cache.set(value: dataToCache, key: "data")
    }
}

This function will then be used like so

Using cache function:

   var storyDataJson = SwiftyJSON.JSON(myresponse)

   self.stories = storyDataJson["stories"]

   self.cacheStory(storyDataJson["stories"])

This doesn't actually do anything and I'm currently getting a warning

Cast from 'JSON' to unrelated type 'JSON' always fails

So when trying to get this data using the function below the application doesn't even build.

Retrieval function:

func getCachedStory(key: String) {
    let cache = Shared.JSONCache
    cache.fetch(key: key).onSuccess { data in

        if let d = data {
             print(d)
        } else {
            print("Nowt")
        }
    }
}

Not too sure if it's even possible to use these two libraries together.

0

There are 0 best solutions below