Skip to content

Commit 0eb718b

Browse files
authored
Fix rare crashes in IOSBackgroundTaskScheduler with on entering callbacks (#3751)
1 parent 1aa5226 commit 0eb718b

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1515
- Add `Components.isReactionPushEmojisEnabled` to control whether an emoji should be set when adding a reaction [#3738](https://github.com/GetStream/stream-chat-swift/pull/3738)
1616
- Add `MessageNotificationContent.reaction` that can be used by Notification Service Extension [#3738](https://github.com/GetStream/stream-chat-swift/pull/3738)
1717
- Add average message response time for users [#3739](https://github.com/GetStream/stream-chat-swift/pull/3739)
18+
### 🐞 Fixed
19+
- Fix rare crashes in `IOSBackgroundTaskScheduler` with on entering callbacks [#3751](https://github.com/GetStream/stream-chat-swift/pull/3751)
1820

1921
## StreamChatUI
2022
### 🐞 Fixed

Sources/StreamChat/WebSocketClient/BackgroundTaskScheduler.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ class IOSBackgroundTaskScheduler: BackgroundTaskScheduler {
8282
onEnteringBackground: @escaping () -> Void,
8383
onEnteringForeground: @escaping () -> Void
8484
) {
85-
self.onEnteringForeground = onEnteringForeground
86-
self.onEnteringBackground = onEnteringBackground
85+
queue.sync {
86+
self.onEnteringForeground = onEnteringForeground
87+
self.onEnteringBackground = onEnteringBackground
88+
}
8789

8890
NotificationCenter.default.addObserver(
8991
self,
@@ -101,8 +103,10 @@ class IOSBackgroundTaskScheduler: BackgroundTaskScheduler {
101103
}
102104

103105
func stopListeningForAppStateUpdates() {
104-
onEnteringForeground = {}
105-
onEnteringBackground = {}
106+
queue.sync {
107+
self.onEnteringForeground = {}
108+
self.onEnteringBackground = {}
109+
}
106110

107111
NotificationCenter.default.removeObserver(
108112
self,
@@ -118,11 +122,13 @@ class IOSBackgroundTaskScheduler: BackgroundTaskScheduler {
118122
}
119123

120124
@objc private func handleAppDidEnterBackground() {
121-
onEnteringBackground()
125+
let callback = queue.sync { onEnteringBackground }
126+
callback()
122127
}
123128

124129
@objc private func handleAppDidBecomeActive() {
125-
onEnteringForeground()
130+
let callback = queue.sync { onEnteringForeground }
131+
callback()
126132
}
127133

128134
deinit {

Tests/StreamChatTests/WebSocketClient/BackgroundTaskScheduler_Tests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ final class IOSBackgroundTaskScheduler_Tests: XCTestCase {
7575
XCTAssertEqual(3, endTaskCallCount)
7676
}
7777

78+
func test_callingAppStateUpdatesConcurretly() {
79+
let scheduler = IOSBackgroundTaskScheduler()
80+
DispatchQueue.concurrentPerform(iterations: 100) { index in
81+
if index.quotientAndRemainder(dividingBy: 2).remainder == 0 {
82+
scheduler.startListeningForAppStateUpdates(onEnteringBackground: {}, onEnteringForeground: {})
83+
} else {
84+
scheduler.stopListeningForAppStateUpdates()
85+
}
86+
}
87+
}
88+
7889
// MARK: - Mocks
7990

8091
class IOSBackgroundTaskSchedulerMock: IOSBackgroundTaskScheduler {

0 commit comments

Comments
 (0)