Skip to content

Commit

Permalink
fix(ios): fix uimanager and bundle operation queue's crash
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and hippy-actions[bot] committed Oct 23, 2023
1 parent 848ef40 commit 61c41d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 18 additions & 12 deletions framework/ios/base/bundleoperations/HippyBundleOperationQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation HippyBundleOperationQueue
- (instancetype)init {
self = [super init];
if (self) {
_ops = [NSMutableArray arrayWithCapacity:8];
_ops = [NSMutableArray array];
}
return self;
}
Expand All @@ -43,22 +43,27 @@ - (void)addOperations:(NSArray<NSOperation *> *)ops {
for (NSOperation *op in ops) {
if ([op isReady]) {
[op addObserver:self forKeyPath:@"finished" options:NSKeyValueObservingOptionNew context:NULL];
[_ops addObject:op];
@synchronized (self) {
[_ops addObject:op];
}
[op start];
}
else if ([op isCancelled]) {

}
else {
} else if ([op isCancelled]) {
// do nothing
} else {
[op addObserver:self forKeyPath:@"ready" options:NSKeyValueObservingOptionNew context:NULL];
[op addObserver:self forKeyPath:@"finished" options:NSKeyValueObservingOptionNew context:NULL];
[_ops addObject:op];
@synchronized (self) {
[_ops addObject:op];
}
}
}
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
NSNumber *value = [change objectForKey:NSKeyValueChangeNewKey];
if (!value) {
return;
Expand All @@ -71,9 +76,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if ([keyPath isEqualToString:@"ready"] && status) {
[op removeObserver:self forKeyPath:@"ready" context:NULL];
[op start];
}
else if ([keyPath isEqualToString:@"finished"] && status) {
[_ops removeObject:object];
} else if ([keyPath isEqualToString:@"finished"] && status) {
@synchronized (self) {
[_ops removeObject:object];
}
[op removeObserver:self forKeyPath:@"finished" context:NULL];
}
}
Expand Down
13 changes: 5 additions & 8 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ - (void)initContext {
}

- (void)invalidate {
_pendingUIBlocks = nil;
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
HippyUIManager *strongSelf = weakSelf;
Expand Down Expand Up @@ -684,24 +683,22 @@ - (void)flushUIBlocksOnRootNode:(std::weak_ptr<RootNode>)rootNode {
int32_t rootTag = strongRootNode->GetId();
NSArray<HippyViewManagerUIBlock> *previousPendingUIBlocks = _pendingUIBlocks;
_pendingUIBlocks = [NSMutableArray new];
__weak HippyUIManager *weakManager = self;
__weak __typeof(self)weakSelf = self;
if (previousPendingUIBlocks.count) {
// Execute the previously queued UI blocks
dispatch_async(dispatch_get_main_queue(), ^{
if (weakManager) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if (strongSelf) {
HP_PERF_LOG("flushUIBlocksOnRootNode on main thread(random id:%u)",rand);
HippyUIManager *strongSelf = weakManager;
NSDictionary<NSNumber *, UIView *> *viewReg =
[strongSelf->_viewRegistry componentsForRootTag:@(rootTag)];
NSDictionary<NSNumber *, UIView *> *viewReg = [strongSelf.viewRegistry componentsForRootTag:@(rootTag)];
@try {
for (HippyViewManagerUIBlock block in previousPendingUIBlocks) {
block(strongSelf, viewReg);
}
} @catch (NSException *exception) {
HippyLogError(@"Exception thrown while executing UI block: %@", exception);
}
HP_PERF_LOG("flushUIBlocksOnRootNode on main thread done, block count:%d(random id:%u)", previousPendingUIBlocks.count, rand);
}
});
}
Expand Down

0 comments on commit 61c41d8

Please sign in to comment.