Skip to content

Commit ea59f7c

Browse files
authored
Merge pull request #1276 from jbaez/dialog-fix
Fix crash when Dialog builder already deallocated when callback executed
2 parents 059ed9c + 24e9d6d commit ea59f7c

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Sources/iOS/Dialogs/Dialog.swift

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ open class Dialog: NSObject {
137137
@discardableResult
138138
open func positive(_ title: String?, handler: (() -> Void)?) -> Dialog {
139139
dialogView.positiveButton.title = title
140-
controller.didTapPositiveButtonHandler = { [unowned self] in
141-
self.delegate?.dialog?(self, didTapPositive: self.controller.dialogView.positiveButton)
140+
controller.didTapPositiveButtonHandler = { [weak self] in
141+
guard let strongSelf = self else { return }
142+
strongSelf.delegate?.dialog?(strongSelf, didTapPositive: strongSelf.controller.dialogView.positiveButton)
142143
handler?()
143144
}
144145
return self
@@ -153,8 +154,9 @@ open class Dialog: NSObject {
153154
@discardableResult
154155
open func negative(_ title: String?, handler: (() -> Void)?) -> Dialog {
155156
dialogView.negativeButton.title = title
156-
controller.didTapNegativeButtonHandler = { [unowned self] in
157-
self.delegate?.dialog?(self, didTapNegative: self.controller.dialogView.negativeButton)
157+
controller.didTapNegativeButtonHandler = { [weak self] in
158+
guard let strongSelf = self else { return }
159+
strongSelf.delegate?.dialog?(strongSelf, didTapNegative: strongSelf.controller.dialogView.negativeButton)
158160
handler?()
159161
}
160162
return self
@@ -169,8 +171,9 @@ open class Dialog: NSObject {
169171
@discardableResult
170172
open func neutral(_ title: String?, handler: (() -> Void)?) -> Dialog {
171173
dialogView.neutralButton.title = title
172-
controller.didTapNeutralButtonHandler = { [unowned self] in
173-
self.delegate?.dialog?(self, didTapNeutral: self.controller.dialogView.neutralButton)
174+
controller.didTapNeutralButtonHandler = { [weak self] in
175+
guard let strongSelf = self else { return }
176+
strongSelf.delegate?.dialog?(strongSelf, didTapNeutral: strongSelf.controller.dialogView.neutralButton)
174177
handler?()
175178
}
176179
return self
@@ -185,8 +188,9 @@ open class Dialog: NSObject {
185188
@discardableResult
186189
open func isCancelable(_ value: Bool, handler: (() -> Void)? = nil) -> Dialog {
187190
controller.isCancelable = value
188-
controller.didCancelHandler = { [unowned self] in
189-
self.delegate?.dialogDidCancel?(self)
191+
controller.didCancelHandler = { [weak self] in
192+
guard let strongSelf = self else { return }
193+
strongSelf.delegate?.dialogDidCancel?(strongSelf)
190194
handler?()
191195
}
192196
return self
@@ -200,8 +204,9 @@ open class Dialog: NSObject {
200204
*/
201205
@discardableResult
202206
open func shouldDismiss(handler: ((DialogView, Button?) -> Bool)?) -> Dialog {
203-
controller.shouldDismissHandler = { [unowned self] dialogView, button in
204-
let d = self.delegate?.dialog?(self, shouldDismiss: button) ?? true
207+
controller.shouldDismissHandler = { [weak self] dialogView, button in
208+
guard let strongSelf = self else { return true }
209+
let d = strongSelf.delegate?.dialog?(strongSelf, shouldDismiss: button) ?? true
205210
let h = handler?(dialogView, button) ?? true
206211
return d && h
207212
}
@@ -215,8 +220,9 @@ open class Dialog: NSObject {
215220
*/
216221
@discardableResult
217222
open func willAppear(handler: (() -> Void)?) -> Dialog {
218-
controller.willAppear = { [unowned self] in
219-
self.delegate?.dialogWillAppear?(self)
223+
controller.willAppear = { [weak self] in
224+
guard let strongSelf = self else { return }
225+
strongSelf.delegate?.dialogWillAppear?(strongSelf)
220226
handler?()
221227
}
222228
return self
@@ -229,10 +235,11 @@ open class Dialog: NSObject {
229235
*/
230236
@discardableResult
231237
open func didDisappear(handler: (() -> Void)?) -> Dialog {
232-
controller.didDisappear = { [unowned self] in
233-
self.delegate?.dialogDidDisappear?(self)
238+
controller.didDisappear = { [weak self] in
239+
guard let strongSelf = self else { return }
240+
strongSelf.delegate?.dialogDidDisappear?(strongSelf)
234241
handler?()
235-
self.controller.dialog = nil
242+
strongSelf.controller.dialog = nil
236243
}
237244
return self
238245
}

0 commit comments

Comments
 (0)