I’m new to swift and iOS development in general, However I can’t motivate my capacity to restore the outcome from an API GET ask. I am running a Microsoft SQL server on my companion’s PC and I am remotely getting to it with DreamFactory. DreamFactory is making and dealing with the API for the SQL queries. I am hosting the DreamFactory API web server on my localhost and I am accessing it through my iOS app.
This is what I have so far.
func validId() -> Bool {
var queryId: Int = 0 // just for testing
let table = "SM.Customers"
// this url looks like ( http://localhost:8080/api/v2/db/_table/SM.Customers/1989/?api_key=065dce537)
let url = URL(string: "\(urlString)\(table)/\(self.id)?api_key=\(self.api_key)")!
URLSession.shared.dataTask(with: url, completionHandler: {
(data, response, error) in
if(error != nil){
print(error.debugDescription)
}else{
do{
self.userData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
OperationQueue.main.addOperation {
queryId = self.userData["CustId"] as! Int
print(queryId)
}
}catch let error as NSError{
print(error)
}
}
}).resume()
return (String(queryId) == self.id)
}
I can do things under OperationQueue.main.addOperation {...}
with the data that I pull from the db
, but I can’t really return a value or assign the value to a variable outside of the task
code block. This function will print(queryId)
as 1989
but it will return false
because it’s reading (String(queryId) == self.id)
as (0 == 1989)
.
Any advices how I can resolve this?