我有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{
}
}
}
答案 0 :(得分:0)
尝试使用函数的完成处理程序,或者尝试使用依赖等待的操作队列
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 :(得分:-1)
使用NSFetchedResultsController
它将跟踪核心数据更改并具有委托回调以告知您何时更改您的tableview。
答案 2 :(得分:-1)
这里的问题是你在coreDataAdd
方法中进行异步调用(因为它们应该是网络调用)。您需要在每次网络呼叫完成后停止PKHUD加载程序。
您可以将dispatch_group用于此目的。在dispatch_group_notify里面,请main queue
隐藏加载程序。