fix: Improve thread safety#142
Open
steve228uk wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
onMainhelper to dispatch work to the main thread, executing synchronously if already on the main threadand asynchronously otherwise.
sendEventToJSinExponeaBridgeto dispatch throughonMain, ensuring all in-app message delegatecallbacks and segmentation callbacks reach the React Native bridge on the main thread.
notifyDimensChanged,notifyContentBlockCarouselEvent,notifyContentFilterRequest, andnotifyContentSortRequestinCarouselInAppContentBlockViewProxyto dispatch throughonMain.notifyDimensChangedandnotifyInAppContentBlockEventinInAppContentBlocksPlaceholderto dispatchthrough
onMain.Issues Resolved
SDK delegate callbacks for in-app messages, content blocks, and segmentation arrive on background threads. Calling
RCTEventEmitter.sendEventand UIKit layout APIs (dimension change notifications) from a background thread causesundefined behaviour under the New Architecture / Fabric renderer and can result in crashes or dropped events.
How
DictionaryExtensions.swift: AddsonMain(_:)— a free function matching the pattern used in the upstreamexponea-ios-sdk. When the caller is already on the main thread it executes the block inline; otherwise it schedulesit with
DispatchQueue.main.async. This avoids unnecessary re-queuing for callbacks that already arrive on main.ExponeaBridge.swift:sendEventToJSwraps itseventEmitter?.sendEventcall inonMain, covering all eventemission paths:
inAppMessageShown,inAppMessageError,inAppMessageClickAction,inAppMessageCloseAction, andemitNewSegments.CarouselInAppContentBlockViewProxy.swift/InAppContentBlocksPlaceholder.swift: Eachnotify*methodserialises any SDK reference types to value-safe representations (e.g.
NSDictionary) on the calling thread beforethe
onMainhop, so no SDK objects cross a thread boundary.