2121#import " SAReactNativeRootViewManager.h"
2222#import < React/RCTUIManager.h>
2323
24+ void sensors_reactnative_dispatch_safe_sync (dispatch_queue_t queue,DISPATCH_NOESCAPE dispatch_block_t block) {
25+ if ((dispatch_queue_get_label (DISPATCH_CURRENT_QUEUE_LABEL)) == dispatch_queue_get_label (queue)) {
26+ block ();
27+ } else {
28+ dispatch_sync (queue, block);
29+ }
30+ }
31+
2432@interface SAReactNativeRootViewManager ()
2533
2634@property (nonatomic , strong ) NSPointerArray *rootViews;
2735@property (nonatomic , strong ) NSMutableDictionary <NSNumber *, NSMutableSet *> *viewProperties;
36+ @property (nonatomic , strong ) dispatch_queue_t serialQueue;
2837
2938@end
3039
@@ -44,6 +53,7 @@ - (instancetype)init {
4453 if (self) {
4554 _rootViews = [NSPointerArray weakObjectsPointerArray ];
4655 _viewProperties = [NSMutableDictionary dictionary ];
56+ _serialQueue = dispatch_queue_create (" cn.SensorsData.SAReactNativeRootViewManagerQueue" , DISPATCH_QUEUE_SERIAL);
4757 }
4858 return self;
4959}
@@ -76,20 +86,25 @@ - (void)addViewProperty:(SAReactNativeViewProperty *)property withRootTag:(NSNum
7686 return ;
7787 }
7888
79- NSMutableSet *viewProperties = self.viewProperties [rootTag];
80- if (!viewProperties) {
81- viewProperties = [NSMutableSet set ];
82- self.viewProperties [rootTag] = viewProperties;
83- }
84- [viewProperties addObject: property];
89+ sensors_reactnative_dispatch_safe_sync (self.serialQueue , ^{
90+ NSMutableSet *viewProperties = self.viewProperties [rootTag];
91+ if (!viewProperties) {
92+ viewProperties = [NSMutableSet set ];
93+ self.viewProperties [rootTag] = viewProperties;
94+ }
95+ [viewProperties addObject: property];
96+ });
8597}
8698
8799- (NSSet <SAReactNativeViewProperty *> *)viewPropertiesWithRootTag : (NSNumber *)rootTag {
88- NSSet *viewProperties = self.viewProperties [rootTag];
89- if (!viewProperties) {
90- return nil ;
91- }
92- return [[NSSet alloc ] initWithSet: viewProperties copyItems: YES ];
100+ __block NSSet *viewProperties = nil ;
101+ sensors_reactnative_dispatch_safe_sync (self.serialQueue , ^{
102+ NSSet *tempProperties = self.viewProperties [rootTag];
103+ if (tempProperties) {
104+ viewProperties = [[NSSet alloc ] initWithSet: tempProperties copyItems: YES ];
105+ }
106+ });
107+ return viewProperties;
93108}
94109
95110#pragma mark - utils
@@ -109,9 +124,11 @@ - (void)compact {
109124 [rootTags addObject: rootView.reactTag];
110125 }
111126
112- NSMutableSet *removeTags = [NSMutableSet setWithArray: [self .viewProperties allKeys ]];
113- [removeTags minusSet: rootTags];
114- [self .viewProperties removeObjectsForKeys: removeTags.allObjects];
127+ sensors_reactnative_dispatch_safe_sync (self.serialQueue , ^{
128+ NSMutableSet *removeTags = [NSMutableSet setWithArray: [self .viewProperties allKeys ]];
129+ [removeTags minusSet: rootTags];
130+ [self .viewProperties removeObjectsForKeys: removeTags.allObjects];
131+ });
115132}
116133
117134@end
0 commit comments