Skip to content

Commit 803c93e

Browse files
feat: enhance ReactHostHelper with UI thread management
fix: add missing import for RCTReloadCommand in ReactHostHelper.h refactor: clean up comments and streamline reload methods in ReactNative refactor: update bundle URL handling in ReactNative class
1 parent c6e6671 commit 803c93e

3 files changed

Lines changed: 60 additions & 23 deletions

File tree

ios/Modules/Helper/ReactHostHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
//
77

88
#import <Foundation/Foundation.h>
9+
#import <React/RCTReloadCommand.h>
910

1011
NS_ASSUME_NONNULL_BEGIN
1112

1213
@interface ReactHostHelper : NSObject
1314

1415
- (nullable id) moduleForClass: (Class) clazz;
1516
- (void) reloadClientWithState;
17+
- (void) reload;
18+
- (void) updateBundleURL: (nonnull NSURL*) bundleURL;
1619
- (BOOL) isReactAppActive;
1720
- (void) emitEvent: (nonnull NSString*) eventName payload: (nullable id) payload;
1821

ios/Modules/Helper/ReactHostHelper.mm

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#import "ReactHostHelper.h"
99
#import <ReactCommon/RCTHost.h>
10-
#import <React/RCTReloadCommand.h>
10+
#import <ReactCommon/RCTHost+Internal.h>
1111
#import "RCTDefaultReactNativeFactoryDelegate.h"
1212
#import "RCTReactNativeFactory.h"
1313
#import "MendixNative-Swift.h"
@@ -17,18 +17,34 @@
1717

1818
@implementation ReactHostHelper
1919

20-
- (nullable id) moduleForClass: (Class) clazz {
20+
#pragma mark - Thread Helpers
21+
22+
- (void) runOnUiThread:(dispatch_block_t)block {
2123
if ([NSThread isMainThread]) {
22-
return [self getModuleForClass: clazz];
24+
block();
2325
} else {
24-
__block id result;
25-
dispatch_sync(dispatch_get_main_queue(), ^{
26-
result = [self getModuleForClass: clazz];
27-
});
28-
return result;
26+
dispatch_async(dispatch_get_main_queue(), block);
2927
}
3028
}
3129

30+
- (void) runOnUiThreadSync:(dispatch_block_t)block {
31+
if ([NSThread isMainThread]) {
32+
block();
33+
} else {
34+
dispatch_sync(dispatch_get_main_queue(), block);
35+
}
36+
}
37+
38+
#pragma mark - Module Access
39+
40+
- (nullable id) moduleForClass: (Class) clazz {
41+
__block id result;
42+
[self runOnUiThreadSync:^{
43+
result = [self getModuleForClass: clazz];
44+
}];
45+
return result;
46+
}
47+
3248
- (nullable id) getModuleForClass: (Class) clazz {
3349
RCTHost *reactHost = [self currentHost];
3450
RCTModuleRegistry *moduleRegistry = [reactHost moduleRegistry];
@@ -50,16 +66,26 @@ - (void) reloadClientWithState {
5066
[mxReload emitOnReloadWithState];
5167
}
5268

69+
- (void) reload {
70+
[self runOnUiThread:^{
71+
[[self currentHost] reload];
72+
}];
73+
}
74+
75+
- (void) updateBundleURL:(NSURL *)bundleURL {
76+
[self runOnUiThread:^{
77+
[[self currentHost] setBundleURLProvider:^NSURL * _Nullable{
78+
return bundleURL;
79+
}];
80+
}];
81+
}
82+
5383
- (BOOL)isReactAppActive {
54-
if ([NSThread isMainThread]) {
55-
return [self currentHost] != nil;
56-
} else {
57-
__block bool result;
58-
dispatch_sync(dispatch_get_main_queue(), ^{
59-
result = [self currentHost] != nil;
60-
});
61-
return result;
62-
}
84+
__block BOOL result;
85+
[self runOnUiThreadSync:^{
86+
result = [self currentHost] != nil;
87+
}];
88+
return result;
6389
}
6490

6591
- (void)emitEvent:(nonnull NSString *)eventName payload:(nullable id)payload {

ios/Modules/ReactNative.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ open class ReactNative: NSObject, RCTReloadListener {
8484

8585
let otaBundleUrl = OtaJSBundleFileProvider.getBundleUrl()
8686
if !mendixApp.isDeveloperApp, let otaBundleUrl = otaBundleUrl {
87-
RCTReloadCommandSetBundleURL(otaBundleUrl)
87+
updateBundleURL(otaBundleUrl)
88+
triggerReload()
8889
}
8990

9091
if mendixApp.isDeveloperApp {
@@ -93,15 +94,22 @@ open class ReactNative: NSObject, RCTReloadListener {
9394
if response.status == "SUCCESS", let version = response.runtimeInfo?.version {
9495
MendixBackwardsCompatUtility.update(version)
9596
}
96-
self?.reloadWithBridge()
97+
self?.triggerReload()
9798
}
9899
} else {
99-
reloadWithBridge()
100+
triggerReload()
100101
}
101102
}
102-
103-
private func reloadWithBridge() {
104-
RCTTriggerReloadCommandListeners("Reload command from app")
103+
104+
private func triggerReload() {
105+
showSplashScreen()
106+
// Note: This bypasses RCTReloadListener notifications (only dev menu Cmd+R triggers those)
107+
ReactHostHelper().reload()
108+
}
109+
110+
private func updateBundleURL(_ url: URL) {
111+
self.bundleUrl = url
112+
ReactHostHelper().updateBundleURL(url)
105113
}
106114

107115
public func reloadWithState() {

0 commit comments

Comments
 (0)