gpt4 book ai didi

ios - 仅当来自服务器的数据快速加载到第一个 View Controller 的核心数据中时,如何运行第二个 View Controller

转载 作者:可可西里 更新时间:2023-11-01 02:15:50 26 4
gpt4 key购买 nike

在我的应用程序中,当登录完成时,相同的 VC 数据从服务器加载到 coredata 中,但是在第二个 VC 的下一个 TableView 中将数据从服务器加载到 coredata 时延迟有空列表。所以,我只想在从第一个 VC 加载数据到 coredata 后运行第二个 vc。

for teEmp in json {

if teEmp.valueForKey("department") !== null {
if teEmp.valueForKey("department") as! String == "Technology"{

// start core data

let entityDescription = NSEntityDescription.entityForName("Tech", inManagedObjectContext: self.managedObjectcontext!)
let tech = Employees(entity: entityDescription!,insertIntoManagedObjectContext: self.managedObjectcontext)

tech.id = teEmp.valueForKey("id")! as? String
tech.first_name = teEmp.valueForKey("firstname")! as? String
tech.card_id = (teEmp.valueForKey("cardId")!) as? String
tech.image = (teEmp.valueForKey("image")!) as? String
tech.last_name = (teEmp.valueForKey("lastname")!) as? String
tech.designation = (teEmp.valueForKey("designation")!) as? String
tech.department = (teEmp.valueForKey("department")!) as? String
if teEmp.valueForKey("startedDate") as! String != "null" {
let dateAsString = teEmp.valueForKey("startedDate")
dateFormatter.dateFormat = "MM/dd/YYYY"
let teStartDate = dateFormatter.dateFromString((dateAsString as? String)!)
dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
//dateFormatter.dateFormat = "MMM dd YYYY"
let techStartDay = dateFormatter.stringFromDate(teStartDate!)
tech.started_date = techStartDay as String
}
tech.departure_date = (teEmp.valueForKey("departureDate")!) as? String
tech.responsibilities = (teEmp.valueForKey("responsibilities")!) as? String
tech.address = (teEmp.valueForKey("address")!) as? String
tech.email = (teEmp.valueForKey("email")!) as? String
tech.shift = (teEmp.valueForKey("shift")!) as? String
tech.linkedIn_profile = (teEmp.valueForKey("linkedInProfile")!) as? String
tech.new_employee = (teEmp.valueForKey("newEmployee")!) as? String
tech.departed = (teEmp.valueForKey("departed")!) as? String
tech.supervisor = (teEmp.valueForKey("supervisor")!) as? String

if teEmp.valueForKey("birthDate") as! String != "null" {
let dateAsString = teEmp.valueForKey("birthDate")
dateFormatter.dateFormat = "MM/dd"
let teBirthDate = dateFormatter.dateFromString(dateAsString as! String)
dateFormatter.dateFormat = "MMM dd"
let techBirthMonth = dateFormatter.stringFromDate(teBirthDate!)
tech.birth_date = techBirthMonth
}

tech.privacy = (teEmp.valueForKey("privacy")!) as? String
if (teEmp.valueForKey("privacy")!) as? String == "PUBLIC"{
tech.contact = (teEmp.valueForKey("contact")!) as? String
} else {
tech.contact = "98********"
}



do {
try self.managedObjectcontext!.save()
print("Tech data saved in core data")

} catch let error as NSError
{
print(error.localizedDescription)


}
// end core data

}

}

}

}

}catch let error as NSError {
print(error)
}

if(err != nil) {
print(err!.localizedDescription)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")

}
else {


}

最佳答案

您可以使用 NSNotificationCenter/委托(delegate)模式/传递闭包/ReactiveCocoa 来实现这一点。这是一个使用 NSNotificationCenter 的例子。

do {
try self.managedObjectcontext!.save()
print("Tech data saved in core data")
// Add this line
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.postNotificationName("CoreDataSaved", object: nil)
}

然后转到您的 FirstViewController:在viewdidload方法中,添加如下-

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self,
selector: "ShowSecondVC",
name:"CoreDataSaved",
object: nil
)

并添加推送第二个 View Controller 的功能。

func ShowSecondVC() {
// Show second VC
}

最后,在 deinit block 中从 First VC 中删除通知监听器。

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

关于ios - 仅当来自服务器的数据快速加载到第一个 View Controller 的核心数据中时,如何运行第二个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com