Skip to content

Commit

Permalink
fix(ios): thread race when getting ViewManager classes (#4144)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Dec 4, 2024
1 parent a3f6621 commit 92cb234
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,6 @@
using DomEvent = hippy::DomEvent;
using RootNode = hippy::RootNode;

static NSMutableArray<Class> *HippyViewManagerClasses = nil;
NSArray<Class> *HippyGetViewManagerClasses(HippyBridge *bridge) {
if (!HippyViewManagerClasses) {
NSArray<Class> *classes = bridge.moduleClasses;
NSMutableArray<Class> *viewManagerClasses = [NSMutableArray array];
for (id aClass in classes) {
if ([aClass isSubclassOfClass:HippyViewManager.class]) {
[viewManagerClasses addObject:aClass];
}
}
HippyViewManagerClasses = viewManagerClasses;
}
return HippyViewManagerClasses;
}

using HPViewBinding = std::map<int32_t, std::tuple<std::vector<int32_t>, std::vector<int32_t>>>;

Expand Down Expand Up @@ -206,6 +192,8 @@ @interface HippyUIManager() {
@property (atomic, strong) NSMutableDictionary<NSString *, id> *viewManagers;
/// All extra components
@property (atomic, strong) NSArray<Class> *extraComponents;
/// Cache of all ViewManager classes
@property (nonatomic, strong) NSArray<Class> *viewManagerClasses;

@end

Expand All @@ -220,12 +208,13 @@ - (instancetype)initWithBridge:(HippyBridge *)bridge {
self = [super init];
if (self) {
_bridge = bridge;
[self initContext];
[self setupContext];
[self setupViewManagerClassesCacheWithBridge:bridge];
}
return self;
}

- (void)initContext {
- (void)setupContext {
_shadowViewRegistry = [[HippyComponentMap alloc] initWithComponentsReferencedType:HippyComponentReferenceTypeStrong];
_viewRegistry = [[HippyComponentMap alloc] initWithComponentsReferencedType:HippyComponentReferenceTypeWeak];
_viewRegistry.requireInMainThread = YES;
Expand All @@ -238,6 +227,16 @@ - (void)initContext {
HippyScreenSize();
}

- (void)setupViewManagerClassesCacheWithBridge:(HippyBridge *)bridge {
NSMutableArray<Class> *viewManagerClasses = [NSMutableArray array];
for (Class aClass in bridge.moduleClasses) {
if ([aClass isSubclassOfClass:HippyViewManager.class]) {
[viewManagerClasses addObject:aClass];
}
}
_viewManagerClasses = viewManagerClasses;
}

- (void)invalidate {
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -704,8 +703,7 @@ - (__kindof HippyViewManager *)viewManagerForViewName:(NSString *)viewName {
}

// Second, read the default view manager classes.
HippyBridge *strongBridge = self.bridge;
NSArray<Class> *classes = HippyGetViewManagerClasses(strongBridge);
NSArray<Class> *classes = self.viewManagerClasses;
NSMutableDictionary *defaultViewManagerClasses = [NSMutableDictionary dictionaryWithCapacity:[classes count]];
for (Class cls in classes) {
HippyAssert([cls respondsToSelector:@selector(moduleName)],
Expand Down

0 comments on commit 92cb234

Please sign in to comment.