Fix intermittent crash

If a status in a conversation view controller creates a work item to
update the timestamp in 1 minute, but the view controller is deinit'd
before that time elapses, the mastodonController instance will be nil,
resulting in a crash.

The DispatchWorkItems's are cancelled by the respective cell deinit
methods. But if the work item has already begun, cancelling it has no
effect, potentially leading to a crash in the conditions described above
are true. Using a weak reference to self fixes this.

Additionally, don't unnecessarily recreate the work items every time.
They don't capture any local variables, only self, so nothing changes.
This commit is contained in:
Shadowfacts 2020-03-01 18:33:44 -05:00
parent d4ca39761e
commit 244659c262
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 16 additions and 8 deletions

View File

@ -115,8 +115,10 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
delay = nil delay = nil
} }
if let delay = delay { if let delay = delay {
updateTimestampWorkItem = DispatchWorkItem { [unowned self] in if updateTimestampWorkItem == nil {
self.updateTimestamp() updateTimestampWorkItem = DispatchWorkItem { [weak self] in
self?.updateTimestamp()
}
} }
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
} else { } else {

View File

@ -104,8 +104,10 @@ class FollowNotificationGroupTableViewCell: UITableViewCell {
delay = nil delay = nil
} }
if let delay = delay { if let delay = delay {
updateTimestampWorkItem = DispatchWorkItem { [unowned self] in if updateTimestampWorkItem == nil {
self.updateTimestamp() updateTimestampWorkItem = DispatchWorkItem { [weak self] in
self?.updateTimestamp()
}
} }
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
} else { } else {

View File

@ -78,8 +78,10 @@ class FollowRequestNotificationTableViewCell: UITableViewCell {
delay = nil delay = nil
} }
if let delay = delay { if let delay = delay {
updateTimestampWorkItem = DispatchWorkItem { [unowned self] in if updateTimestampWorkItem == nil {
self.updateTimestamp() updateTimestampWorkItem = DispatchWorkItem { [weak self] in
self?.updateTimestamp()
}
} }
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
} else { } else {

View File

@ -110,8 +110,10 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell {
delay = nil delay = nil
} }
if let delay = delay { if let delay = delay {
updateTimestampWorkItem = DispatchWorkItem { [unowned self] in if updateTimestampWorkItem == nil {
self.updateTimestamp() updateTimestampWorkItem = DispatchWorkItem { [weak self] in
self?.updateTimestamp()
}
} }
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
} else { } else {