Skip to content

Commit

Permalink
[core][ios] Give precedence to UIBackgroundFetchResult.newData over…
Browse files Browse the repository at this point in the history
… `.failed` in ExpoAppDelegate (#19311)
  • Loading branch information
ferologics committed Sep 30, 2022
1 parent ca94fe5 commit c7e363a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/expo-modules-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Removed the hard dependency to Hermes or JSC in _libexpo-modules-core.so_ on Android and fixed the broken support for react-native-v8. ([#18899](https://github.com/expo/expo/pull/18899) by [@kudo](https://github.com/kudo))
- Update gradle excludes to fix detox tests. ([#19254](https://github.com/expo/expo/pull/19254) by [@esamelson](https://github.com/esamelson))
- Fixed event listeners do not work when running with remote debugging mode on iOS. ([#19211](https://github.com/expo/expo/pull/19211) by [@kudo](https://github.com/kudo))
- Give precedence to `UIBackgroundFetchResult.newData` over `.failed` in proxied `ExpoAppDelegate.swift` completion handlers. ([#19311](https://github.com/expo/expo/pull/19311) by [@ferologics](https://github.com/ferologics))

### 💡 Others

Expand Down
34 changes: 24 additions & 10 deletions packages/expo-modules-core/ios/AppDelegates/ExpoAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,28 @@ open class ExpoAppDelegate: UIResponder, UIApplicationDelegate {
let selector = #selector(application(_:didReceiveRemoteNotification:fetchCompletionHandler:))
let subs = subscribers.filter { $0.responds(to: selector) }
var subscribersLeft = subs.count
var fetchResult: UIBackgroundFetchResult = .noData
let dispatchQueue = DispatchQueue(label: "expo.application.remoteNotification", qos: .userInteractive)
var failedCount = 0
var newDataCount = 0

let handler = { (result: UIBackgroundFetchResult) in
dispatchQueue.sync {
if result == .failed {
fetchResult = .failed
} else if fetchResult != .failed && result == .newData {
fetchResult = .newData
failedCount += 1
} else if result == .newData {
newDataCount += 1
}

subscribersLeft -= 1

if subscribersLeft == 0 {
completionHandler(fetchResult)
if newDataCount > 0 {
completionHandler(.newData)
} else if failedCount > 0 {
completionHandler(.failed)
} else {
completionHandler(.noData)
}
}
}
}
Expand Down Expand Up @@ -216,21 +223,28 @@ open class ExpoAppDelegate: UIResponder, UIApplicationDelegate {
let selector = #selector(application(_:performFetchWithCompletionHandler:))
let subs = subscribers.filter { $0.responds(to: selector) }
var subscribersLeft = subs.count
var fetchResult: UIBackgroundFetchResult = .noData
let dispatchQueue = DispatchQueue(label: "expo.application.performFetch", qos: .userInteractive)
var failedCount = 0
var newDataCount = 0

let handler = { (result: UIBackgroundFetchResult) in
dispatchQueue.sync {
if result == .failed {
fetchResult = .failed
} else if fetchResult != .failed && result == .newData {
fetchResult = .newData
failedCount += 1
} else if result == .newData {
newDataCount += 1
}

subscribersLeft -= 1

if subscribersLeft == 0 {
completionHandler(fetchResult)
if newDataCount > 0 {
completionHandler(.newData)
} else if failedCount > 0 {
completionHandler(.failed)
} else {
completionHandler(.noData)
}
}
}
}
Expand Down

0 comments on commit c7e363a

Please sign in to comment.