0
votes

I am trying to use specific capabilities not available in the available iOS SDK for Parse Platform (Server), but that I know are available with the REST API. Specifically to use a DISTINCT query.

Using the Parse Dashboard and REST API client app on my dev computer (Rested.app), I have verified the following query completes as expected:

curl -X GET \
-H "X-Parse-Application-Id: someAppID" \
-H "X-Parse-Master-Key: someKey" \
-G \
--data-urlencode "distinct=TeeTime" \
http://somehost:1337/parse/aggregate/CompEntry

Which successfully returns data:

{ "results": [ { "__type": "Date", "iso": "2020-08-29T07:00:00.000Z" }, { "__type": "Date", "iso": "2020-08-29T07:09:00.000Z" } ] }

The original data is from, which has 3 rows, 2 of which share the same TeeTime:

Parse Dashboard screenshot

And a screenshot of the output from the Rested.app:

Rested.app Output screenshot

Now I am trying to convert this for my Swift / iOS project.

I am trying to move the downloaded data into a new struct to represent the object(s), using the Codable/Decodable approach and matching the JSON property names. The code I have so far is below (placed some comments inline too). The Struct definitions occur in separate .swift files, but so long as outside the main ViewController definition.

struct TeeTimeData: Codable {
    let results: [Results]
}

struct Results: Codable {
    let __type: String
    let iso: String // TODO: THIS SHOULD BE A DIFFERENT DATA TYPE - I HAVE PICKED HARDER DATA TYPE TO START WITH!
}

Then within the main ViewController struct:

class ViewController: UIViewController {

    @IBAction func buttonGetTeeTimes(_ sender: UIButton) {
        
        if let url = URL(string: "http://somehost:1337/parse/aggregate/CompEntry") {
            var request = URLRequest(url: url)
            request.addValue("someAppID", forHTTPHeaderField: "X-Parse-Application-Id")
            request.addValue("someKey", forHTTPHeaderField: "X-Parse-Master-Key")
            request.httpMethod = "GET"
            let params = ["distinct": "TeeTime"] // TODO: THIS VAR IS NOT ACTUALLY BEING TIED TO THE REQUEST EITHER - 2nd PROBLEM...
            
            let session = URLSession(configuration: .default)
            
            let requestTask = session.dataTask(with: request) { (data, response, error) in
                
                if error != nil {
                    print("API Error: \(error)")
                }
                
                if let dataUnwrap = data {
                    // TODO: MOVE THIS INTO NEW CLASS (DataModel, etc)
                    let decoder = JSONDecoder()
                    
                    do {
                        let decodedData = try decoder.decode(TeeTimeData.self, from: dataUnwrap)
                        
                        print(decodedData)
                        
                    } catch {
                        print("Decode Error: \(error)")
                    }
                    
                    
                }
            }
            
            requestTask.resume()
            
        }
    }
}

And the console output is:

Decode Error: keyNotFound(CodingKeys(stringValue: "__type", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "__type", intValue: nil) ("__type").", underlyingError: nil))

My first guess is the 2 underscores, "__type", at the start of the property definition?

1
There is no key __type in your response JSON. - gcharita
print("Decode Error: \(error)") => print("Decode Error: \(error)"); print("With response: \(String(data: data, encoding: .utf8))")might help. - Larme
The dummy data doesn't cause that error, so the real data must be different. - vadian
"--data-urlencode "distinct=TeeTime" Doesn't it means that it'd add &distinct=TeeTime to the URL? You aren't doing it in your code, no? So maybe that's why the response is different (and why you might want to show the returned data as String). - Larme
I meant ?distinct=TeeTime, sorry, typo. Check en.wikipedia.org/wiki/Query_string and developer.apple.com/documentation/foundation/urlqueryitem (and related StackOverflow questions if needed) to build it with your parameters. - Larme

1 Answers

0
votes

Answer provided by @Larme in the comments to the opening post. Contributions from @gcharita that my syntax should have worked if it was the same structure as the other tests I was performing (it wasn't - see below).

The error was occurring because the results being returned was a different JSON structure because the distinct=TeeTime filter wasn't being applied. So it was returning a JSON object with all the rows of data from the class, not those specified in my TeeTimeData struct object.

Appending ?distinct=TeeTime to the end of the URL string resolved the issue and returns:

Decoded data: TeeTimeData(results: [Parse_2.Results(__type: "Date", iso: "2020-08-29T07:00:00.000Z"), Parse_2.Results(__type: "Date", iso: "2020-08-29T07:09:00.000Z")])

It also means I no longer require the let params = ["distinct": "TeeTime"] code as well.