@IBAction func sendSweet(sender: UIBarButtonItem) {
var inputTextField: UITextField?
let alert = UIAlertController(title: "New sweet", message: "Enter a sweet", preferredStyle: .alert)
alert.addTextField { (textField: UITextField) in
textField.placeholder = "Your sweet"
inputTextField = textField
}
let sendAction = UIAlertAction(title: "Send", style: .default, handler: {
[weak self] (alertAction: UIAlertAction) in
guard let strongSelf = self else { return }
if inputTextField?.text != "" {
let newSweet = CKRecord(recordType: "Sweet")
newSweet["content"] = inputTextField?.text as CKRecordValue?
let publicData = CKContainer.default().publicCloudDatabase
publicData.save(newSweet, completionHandler: {
(record: CKRecord?, error: Error?) in
if error == nil {
// we want ui code to dispatch asychronously in main thread
DispatchQueue.main.async {
strongSelf.tableView.beginUpdates()
strongSelf.sweets.insert(newSweet, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
strongSelf.tableView.insertRows(at: [indexPath], with: .top)
strongSelf.tableView.endUpdates()
}
} else {
if let error = error {
print(error.localizedDescription)
return
}
}
})
}
})
alert.addAction(sendAction)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
콜백 지옥의 매우 상단에있는이
[weak self]
&guard let strongSelf
공약수의를 통해 모든 밖으로 강한 참조주기를 방지합니까 알고 싶어 비동기 콜백. 나는 콜백 내부에서 참조하는 객체가 성공적으로deinit
을 참조 할 수 있다면 강한 참고주기가없는 것을 나타내는 좋은 징조를 의미하는 책에서 또 하나의 게시물을 읽었다.이런 종류의 콜백을 방지하려면 어떻게해야합니까? 내가 놓친 일부 독서물이나 주제로 안내 할 수 있습니까? 자바 스크립트의 약속 체인 연결 구문 같은 건?
이, 참조 사이클 만 내가 읽고 다른 블로그 게시물을 생각 나게 객체 계층에서 두 방향으로 참조하려고 할 때 발생하지만 단일 방향으로 참조되지는 않습니다. –
즉 '주기'의 정의. – shallowThought