我在viewController中有这个代码。问题是当两个函数调用时加载CKRecord Object中的所有数据。下一行代码执行并打印0作为记录计数。我希望Swift等待两个函数完成然后打印他们的计数。需要帮助来解决这个问题。
override func viewDidLoad() {
super.viewDidLoad()
view1.layer.borderWidth = 1
view1.layer.cornerRadius = 10
tableView.layer.borderWidth = 1
tableView.layer.cornerRadius = 10
loadNewData() // function that loads all partyAccounts
fetchPartyAccounts() // function to load all transactions
print(loadTransactionData.count)
print(partyAccountsData.count)
}
func loadNewData() {
let qry = CKQuery(recordType: "PartyAccounts", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Party_ID", ascending: true)]
publicDB.perform(qry, inZoneWith: nil) { (results, error) in
DispatchQueue.main.async {
if let rcds = results {
self.partyAccountsData = rcds
}
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
self.tableView.reloadData()
}
func fetchPartyAccounts() {
let qry = CKQuery(recordType: "Transactions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Party", ascending: true)]
publicDB.perform(qry, inZoneWith: nil) { (results, error) in
DispatchQueue.main.async {
if let rcds = results {
self.loadTransactionData = rcds
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
}
self.tableView.reloadData()
}
答案 0 :(得分:0)
做这样的事情..
variable_A= your_First_Function()
variable_B= your_Second_Function()
print(variable_A)
print(variable_B
希望这能解决您的问题。虽然不确定,但你可以尝试一下。如果有效,请告诉我。
答案 1 :(得分:0)
你可以试试这个:
override func viewDidLoad() {
super.viewDidLoad()
view1.layer.borderWidth = 1
view1.layer.cornerRadius = 10
tableView.layer.borderWidth = 1
tableView.layer.cornerRadius = 10
loadNewData(){
print(partyAccountsData.count)
} // function that loads all partyAccounts
fetchPartyAccounts() {
print(loadTransactionData.count)
} // function to load all transactions
// print(loadTransactionData.count)
// print(partyAccountsData.count)
}
func loadNewData(callback:@escaping () -> Void) {
let qry = CKQuery(recordType: "PartyAccounts", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Party_ID", ascending: true)]
publicDB.perform(qry, inZoneWith: nil) { (results, error) in
DispatchQueue.main.async {
if let rcds = results {
self.partyAccountsData = rcds
self.tableView.reloadData()
callback()
}
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
}
func fetchPartyAccounts(callback:@escaping () -> Void) {
let qry = CKQuery(recordType: "Transactions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Party", ascending: true)]
publicDB.perform(qry, inZoneWith: nil) { (results, error) in
DispatchQueue.main.async {
if let rcds = results {
self.loadTransactionData = rcds
callback()
self.tableView.reloadData()
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
}
}