Learn Courses My Dashboard

WordPress JSON is caching

I am building an app that is reading data from a WordPress database using JSON. I have the datafeed working fine, the problem is that the JSON data is caching so that changes or additions to the data are never reflected in the JSON feed.

I’ve googled a ton and have added an HTTP header request for no caching but seem to still be getting cached results. Any advice?

Here is some sample code I’m working with right now.

func getPosts() {

    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration)
    let url = URL(string: "https://sjconnect.app/wp-json/wp/v2/posts/")
    var request: URLRequest = URLRequest(url: url!)
    request.httpMethod = "GET" 
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.addValue("no-store, max-age=0", forHTTPHeaderField: "Cache-Control")
    let task = session.dataTask(with: url!) { data, response, error in
        guard (response as? HTTPURLResponse) != nil else {
            print("Invalid http response")
        guard error == nil else {
            print("Error while making request")
        do {
            let serializedData = try JSONSerialization.jsonObject(with: data!, options: [])
            var postSlugs: Array<String> = []

            for post in serializedData as! Array<NSDictionary> {
                postSlugs.append(post["slug"] as! String)
            DispatchQueue.main.async {
                self.allPosts = postSlugs
        } catch {
            print("Error during JSON serialization: \(error.localizedDescription)")

I stand corrected but the configuration I used was"

let configuration = URLSessionConfiguration.ephemeral