Skip to content

fix: Improve thread safety#142

Open
steve228uk wants to merge 1 commit into
exponea:mainfrom
steve228uk:thread-safety
Open

fix: Improve thread safety#142
steve228uk wants to merge 1 commit into
exponea:mainfrom
steve228uk:thread-safety

Conversation

@steve228uk
Copy link
Copy Markdown

Summary

  • Adds an onMain helper to dispatch work to the main thread, executing synchronously if already on the main thread
    and asynchronously otherwise.
  • Updates sendEventToJS in ExponeaBridge to dispatch through onMain, ensuring all in-app message delegate
    callbacks and segmentation callbacks reach the React Native bridge on the main thread.
  • Updates notifyDimensChanged, notifyContentBlockCarouselEvent, notifyContentFilterRequest, and
    notifyContentSortRequest in CarouselInAppContentBlockViewProxy to dispatch through onMain.
  • Updates notifyDimensChanged and notifyInAppContentBlockEvent in InAppContentBlocksPlaceholder to dispatch
    through onMain.

Issues Resolved

SDK delegate callbacks for in-app messages, content blocks, and segmentation arrive on background threads. Calling
RCTEventEmitter.sendEvent and UIKit layout APIs (dimension change notifications) from a background thread causes
undefined behaviour under the New Architecture / Fabric renderer and can result in crashes or dropped events.

How

  • DictionaryExtensions.swift: Adds onMain(_:) — a free function matching the pattern used in the upstream
    exponea-ios-sdk. When the caller is already on the main thread it executes the block inline; otherwise it schedules
    it with DispatchQueue.main.async. This avoids unnecessary re-queuing for callbacks that already arrive on main.
  • ExponeaBridge.swift: sendEventToJS wraps its eventEmitter?.sendEvent call in onMain, covering all event
    emission paths: inAppMessageShown, inAppMessageError, inAppMessageClickAction, inAppMessageCloseAction, and
    emitNewSegments.
  • CarouselInAppContentBlockViewProxy.swift / InAppContentBlocksPlaceholder.swift: Each notify* method
    serialises any SDK reference types to value-safe representations (e.g. NSDictionary) on the calling thread before
    the onMain hop, so no SDK objects cross a thread boundary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant