【发布时间】:2017-07-28 21:40:57
【问题描述】:
我有coredata 表,当用户登录时想要在其中插入大数据。我有核心数据添加功能,我有 PKHUD 加载我尝试使用它们,但功能 不要等待 完成所有数据插入,进入 Profile viewcontroller,我想在核心数据添加时等待功能完成PKHUD加载后继续..我该如何解决?
我的代码在下面。
@IBAction func login (sender: UIButton){
if prefs.string(forKey: "name") == "John"{
}else{
if prefs.string(forKey: "adddata") == "yes"{
// LOADING BAR STARDED
PKHUD.sharedHUD.contentView = PKHUDProgressView()
PKHUD.sharedHUD.show()
// HERE MY ADD CORE DATA FUNCTIONS
coreDataAdd(where : "User")
coreDataAdd(where : "Profile")
coreDataAdd(where : "Games")
if prefs.string(forKey: "go") == "yes"{
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Profile") as! profileViewController
self.present(nextViewController, animated:true, completion:nil)
}
// LOADING BAR END
DispatchQueue.main.async {
PKHUD.sharedHUD.hide(animated:false);
}
}
}
}
下面是我的功能
func coreDataAdd(where : String) {
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()
}
}
}else{
}
}
}
【问题讨论】:
标签: ios swift core-data swift3