try using completion handler for your function or may try Operation queue with dependency to wait
coreDataAdd (where : "User", onCompletion: { (isFinished) in
if isFinished {
coreDataAdd (where : "Profile", onCompletion: { (isFinished) in
if isFinished {
coreDataAdd (where : "Profile", onCompletion: { (isFinished) in
if isFinished
//Here segue to the next view
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Profile") as! profileViewController
self.present(nextViewController, animated:true, completion:nil)
})
}
}
})
})
func coreDataAdd(where : String, onCompletion: @escaping (Bool) -> Void) {
let headers: HTTPHeaders = [
"Accept": "text/html",
"Content-Type" : "application/x-www-form-urlencoded"
]
let parameters = [
"user": "\(prefs.string(forKey: "user")!)"
] as [String : String]
Alamofire.request(geturl, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers)
.responseData{ response in
let json = JSON(data:response.result.value!)
let success = json["Success"]["status"].boolValue
let addJson = json["\(where)_list"].array
if success == true {
if where == "User" {
for users in addJson! {
let addUser = NSEntityDescription.insertNewObject(forEntityName: "\(where)", into:context) as! User
addUser.id = users["id"].int32Value
addUser.name = users["name"].stringValue
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
onCompletion(true)
}
}else{
}
}
}
1
solved How to wait core data add functions step by step in swift 3