I am new to Xcode and programming languages and I need your help. I am working on a Message application and I'm unable to use a Completion Handler Block. Here is my code :
@IBAction func SendButton(_ sender: AnyObject) {
if self.textfield.text != "" {
let mess = CKRecord(recordType: "Message")
mess["content"] = textfield.text as CKRecordValue?
let publicdata = CKContainer.default().publicCloudDatabase
publicdata.save(mess, completionHandler: {(record, error) -> Void in
if error == nil {
let indexpath = NSIndexPath(item: self.chat.count, section: 0)
CATransaction.begin()
CATransaction.setDisableActions(true)
self.CollView.performBatchUpdates ({
self.chat.insert(mess, at: self.chat.count)
self.CollView.insertItems(at: [indexpath as IndexPath])
}, completion: {(true) -> Void in
print("Animation completed")
self.CollView.contentOffset = CGPoint(x: 0, y: 40)
})
CATransaction.commit()
print("SAVED")
}else{
print("error")
}})
}
textfield.text = ""
}
I use CATransaaction to performBatchUpdates, but the Completion Handler Block in the performBatchUpdates method can't be completed. As you can see, it's not the right way to write it and I know it, but I've tried everything I know to achieve it but it won't. The completion :
completion: {(true) -> Void in
print("Animation completed")
self.CollView.contentOffset = CGPoint(x: 0, y: 40)
})
Please help me. Thank you !!
you can try this