I update the user data (this is really simple I just parse a Json and save it to a global variable and in a coredata object) but the problem is that the statements that are after the function are executed before the function.
UpdateUserData()
print(User.user)
In the function above I call a public function that is in another file, but the print throws an empty dictionary and with a breakpoint I noticed that the print statement is executed before the function
UPDATE: this is the method for getting the data
public func UpdateUserData(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Usuarios")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
for dataCoreValue in result as! [NSManagedObject] {
let urlString = "http://13.59.219.129/apostologo/api/v1/usuarios/\(dataCoreValue.value(forKey: "id") as! String)"
print(urlString)
let url = NSURL(string: urlString)!
let urlSession = URLSession.shared
let jsonQuery = urlSession.dataTask(with: url as URL, completionHandler: {
data, response, error -> Void in
if (error != nil) {
}else{
let jsonResult = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary
DispatchQueue.main.async {
Usuario.usuario = ["id":String(describing: (jsonResult["usuarios"] as! NSDictionary)["id_usuario"] as! NSNumber),
"correo":(jsonResult["usuarios"] as! NSDictionary)["correo"] as! String,
"fecha_nacimiento":(jsonResult["usuarios"] as! NSDictionary)["fecha_nacimiento"] as! String,
"nombre":"\((jsonResult["usuarios"] as! NSDictionary)["nombres"] as! String) \((jsonResult["usuarios"] as! NSDictionary)["apellidos"] as! String)",
"tipo":(jsonResult["usuarios"] as! NSDictionary)["tipo"] as! String,
"token": Int((jsonResult["usuarios"] as! NSDictionary)["token"] as! String)!]
DispatchQueue.main.async {
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
dataCoreValue.setValue(Usuario.usuario["token"] as! Int, forKey: "tokens")
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}
}
})
jsonQuery.resume()
}
} catch {
print("Failed")
}
}