Hello everyone I have issue When using `background...
# ask-questions
b
Hello everyone I have issue When using
backgroundSync: true
on iOS, the SSE connection permanently dies after the device loses internet connectivity. Even after connectivity is restored, the
setRefreshHandler
is never called again for any future feature flag changes. The only workaround we found was killing and relaunching the app. My current solution: I listen for network connectivity restoration using
NWPathMonitor
and reinitialize the entire SDK instance when internet comes back:
Copy code
private func setupNetworkMonitor() {
    reachabilityCancellable = networkMonitor.stateSubject
        .removeDuplicates()
        .sink { [weak self] state in
            guard let self else { return }
            switch state {
            case .connected:
                guard self.isNetworkDisconnected else { return }
                self.isNetworkDisconnected = false
                self.initializeSDKInstance() // creates a fresh GrowthBookSDK
            case .notConnected:
                self.isNetworkDisconnected = true
            }
        }
}
My Questions: 1. Does
backgroundSync: true
have any built-in SSE reconnection logic after network loss, or is reinitializing the SDK the expected approach? 2. Is it safe to replace
sdkInstance
with a new instance on every disconnect/reconnect cycle? We're concerned about memory, pending callbacks from the old instance, or any internal state that should be preserved
p
Hi What version of SDK are you using? The SDK does have built-in reconnection logic when
backgroundSync: true
- it should automatically reconnect after network loss. Could you double-check the SSE URL being passed to the SDK is correct and reachable? We've seen cases where an incorrect or malformed SSE URL causes the connection silently fail, which looks exactly like the symptoms you're describing As for reinitializing the SDK instance - yes, it's safe to replace
sdkInstance
with a new one. The SDK doesn't hold global shared state, so the old one will be deallocated normally as long as you don't keep other strong references to it. That said, if the reconnection logic works correctly with the right URL, you shouldn't need the
NWPathMonitor
workaround at all.
b
Hi, I’m currently using SDK version 1.0.62. Here is my
initializeSDKInstance()
implementation:
Copy code
private func initializeSDKInstance() {
    sdkInstance = GrowthBookBuilder(
        apiHost: "<https://cdn.growthbook.io>",
        clientKey: ,
        attributes: [:],
        trackingCallback: { experiment, experimentResult in
            dLog("Experiment Id: ", experiment.key)
            dLog("Variation Id: ", experimentResult.variationId)
        },
        backgroundSync: true)
    .setRefreshHandler(refreshHandler: { isRefreshed in
        dLog(isRefreshed)
    })
    .initializer()
}
p
Version 1.0.62 is quite outdated - there have been a lot of improvements to SSE stability and reconnection logic since then. Could you try updating to the latest version (1.0.97)? That alone may resolve the issue you're experiencing.