Skip to content

Commit

Permalink
fix(ios): delete the old useless cache logic for list and waterfall
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and hippy-actions[bot] committed Dec 22, 2023
1 parent 316ba74 commit e831f31
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 128 deletions.
3 changes: 0 additions & 3 deletions renderer/native/ios/renderer/HippyUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@
/// Get all rootView
- (NSArray<__kindof UIView *> *)rootViews;

/// Purge view from superView
- (void)purgeViewsFromComponentTags:(NSArray<NSNumber *> *)componentTag onRootTag:(NSNumber *)rootTag;

/// Update view with props
- (void)updateView:(NSNumber *)componentTag onRootTag:(NSNumber *)rootTag props:(NSDictionary *)pros;

Expand Down
11 changes: 0 additions & 11 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,6 @@ - (void)purgeChildren:(NSArray<id<HippyComponent>> *)children
}
}
- (void)purgeViewsFromComponentTags:(NSArray<NSNumber *> *)componentTags onRootTag:(NSNumber *)rootTag {
for (NSNumber *componentTag in componentTags) {
UIView *view = [self viewForHippyTag:componentTag onRootTag:rootTag];
HippyComponentMap *componentMap = _viewRegistry;
NativeRenderTraverseViewNodes(view, ^(id<HippyComponent> subview) {
NSAssert(![subview isHippyRootView], @"Root views should not be unregistered");
[componentMap removeComponent:subview forRootTag:rootTag];
});
}
}
- (void)removeChildren:(NSArray<id<HippyComponent>> *)children fromContainer:(id<HippyComponent>)container {
for (id<HippyComponent> removedChild in children) {
[container removeHippySubview:removedChild];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,6 @@ - (void)collectionView:(UICollectionView *)collectionView
}
}

- (void)collectionView:(UICollectionView *)collectionView
didEndDisplayingCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[NativeRenderBaseListViewCell class]]) {
NativeRenderBaseListViewCell *hpCell = (NativeRenderBaseListViewCell *)cell;
if (hpCell.cellView) {
[_cachedItems setObject:[hpCell.cellView hippyTag] forKey:indexPath];
}
}
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NativeRenderBaseListViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

Expand All @@ -290,9 +279,6 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
[_cachedVisibleCellViews setObject:cellView forKey:shadowView.hippyTag];
}

if (cellView) {
[_cachedItems removeObjectForKey:indexPath];
}
HippyAssert([cellView conformsToProtocol:@protocol(ViewAppearStateProtocol)],
@"subviews of NativeRenderBaseListViewCell must conform to protocol ViewAppearStateProtocol");
cell.cellView = cellView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ typedef NS_ENUM(NSInteger, NativeRenderScrollState) {
@protected
NativeRenderWaterfallViewDataSource *_dataSource;

NSMutableDictionary<NSIndexPath *, NSNumber *> *_cachedItems;
NSMapTable<NSNumber *, UIView *> *_cachedVisibleCellViews;

NativeRenderHeaderRefresh *_headerRefreshView;
Expand Down Expand Up @@ -115,10 +114,6 @@ typedef NS_ENUM(NSInteger, NativeRenderScrollState) {
@property (nonatomic, copy) HippyDirectEventBlock onRefresh;
@property (nonatomic, copy) HippyDirectEventBlock onExposureReport;

- (NSUInteger)maxCachedItemCount;

- (NSArray<NSIndexPath *> *)findFurthestIndexPathsFromScreen;

/**
* Initial collection view
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ - (instancetype)initWithFrame:(CGRect)frame {
self.backgroundColor = [UIColor clearColor];
_scrollListeners = [NSHashTable weakObjectsHashTable];
_scrollEventThrottle = 100.f;
_cachedItems = [NSMutableDictionary dictionary];
_cachedVisibleCellViews = [NSMapTable strongToWeakObjectsMapTable];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[self initCollectionView];
Expand Down Expand Up @@ -172,65 +171,6 @@ - (void)setContainBannerView:(BOOL)containBannerView {

#pragma mark Setter & Getter

- (NSUInteger)maxCachedItemCount {
return NSUIntegerMax;
}

- (NSUInteger)differenceFromIndexPath:(NSIndexPath *)indexPath1 againstAnother:(NSIndexPath *)indexPath2 {
NSAssert([NSThread mainThread], @"must be in main thread");
long diffCount = 0;
for (NSUInteger index = MIN([indexPath1 section], [indexPath2 section]); index < MAX([indexPath1 section], [indexPath2 section]); index++) {
diffCount += [_collectionView numberOfItemsInSection:index];
}
diffCount = diffCount + [indexPath1 row] - [indexPath2 row];
return abs(diffCount);
}

- (NSInteger)differenceFromIndexPath:(NSIndexPath *)indexPath againstVisibleIndexPaths:(NSArray<NSIndexPath *> *)visibleIndexPaths {
NSIndexPath *firstIndexPath = [visibleIndexPaths firstObject];
NSIndexPath *lastIndexPath = [visibleIndexPaths lastObject];
NSUInteger diffFirst = [self differenceFromIndexPath:indexPath againstAnother:firstIndexPath];
NSUInteger diffLast = [self differenceFromIndexPath:indexPath againstAnother:lastIndexPath];
return MIN(diffFirst, diffLast);
}

- (NSArray<NSIndexPath *> *)findFurthestIndexPathsFromScreen {
NSUInteger visibleItemsCount = [[self.collectionView visibleCells] count];
NSUInteger maxCachedItemCount = [self maxCachedItemCount] == NSUIntegerMax ? visibleItemsCount * 3 : [self maxCachedItemCount];
NSUInteger cachedCount = [_cachedItems count];
NSInteger cachedCountToRemove = cachedCount > maxCachedItemCount ? cachedCount - maxCachedItemCount : 0;
if (0 != cachedCountToRemove) {
NSArray<NSIndexPath *> *visibleIndexPaths = [_collectionView indexPathsForVisibleItems];
NSArray<NSIndexPath *> *sortedCachedItemKey = [[_cachedItems allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSIndexPath *ip1 = obj1;
NSIndexPath *ip2 = obj2;
NSUInteger ip1Diff = [self differenceFromIndexPath:ip1 againstVisibleIndexPaths:visibleIndexPaths];
NSUInteger ip2Diff = [self differenceFromIndexPath:ip2 againstVisibleIndexPaths:visibleIndexPaths];
if (ip1Diff > ip2Diff) {
return NSOrderedAscending;
}
else if (ip1Diff < ip2Diff) {
return NSOrderedDescending;
}
else {
return NSOrderedSame;
}
}];
NSArray<NSIndexPath *> *result = [sortedCachedItemKey subarrayWithRange:NSMakeRange(0, cachedCountToRemove)];
return result;
}
return nil;
}

- (void)purgeFurthestIndexPathsFromScreen {
NSArray<NSIndexPath *> *furthestIndexPaths = [self findFurthestIndexPathsFromScreen];
//purge view
NSArray<NSNumber *> *objects = [_cachedItems objectsForKeys:furthestIndexPaths notFoundMarker:@(-1)];
[self.renderImpl purgeViewsFromComponentTags:objects onRootTag:self.rootTag];
//purge cache
[_cachedItems removeObjectsForKeys:furthestIndexPaths];
}

- (void)setContentInset:(UIEdgeInsets)contentInset {
_contentInset = contentInset;

Expand Down Expand Up @@ -314,19 +254,6 @@ - (void)didUpdateHippySubviews {
// Do nothing, as subviews is managed by `insertHippySubview:atIndex:` or lazy-created
}

- (void)removeFromHippySuperview {
[super removeFromHippySuperview];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(purgeFurthestIndexPathsFromScreen) object:nil];
[self purgeFurthestIndexPathsFromScreen];
}

- (void)didMoveToWindow {
if (!self.window) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(purgeFurthestIndexPathsFromScreen) object:nil];
[self purgeFurthestIndexPathsFromScreen];
}
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [_dataSource numberOfSection];
Expand All @@ -352,12 +279,8 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
[_cachedVisibleCellViews setObject:cellView forKey:shadowView.hippyTag];
}

if (cellView) {
[_cachedItems removeObjectForKey:indexPath];
}
cell.cellView = cellView;
cellView.parent = self;

return cell;
}

Expand All @@ -383,17 +306,6 @@ - (void)collectionView:(UICollectionView *)collectionView
}
}

- (void)collectionView:(UICollectionView *)collectionView
didEndDisplayingCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[NativeRenderWaterfallViewCell class]]) {
NativeRenderWaterfallViewCell *hpCell = (NativeRenderWaterfallViewCell *)cell;
if (hpCell.cellView) {
[_cachedItems setObject:[hpCell.cellView hippyTag] forKey:indexPath];
}
}
}

#pragma mark - NativeRenderCollectionViewDelegateWaterfallLayout

- (CGSize)collectionView:(UICollectionView *)collectionView
Expand Down Expand Up @@ -462,8 +374,6 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[scrollViewListener scrollViewDidScroll:scrollView];
}
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(purgeFurthestIndexPathsFromScreen) object:nil];
[self performSelector:@selector(purgeFurthestIndexPathsFromScreen) withObject:nil afterDelay:delayForPurgeView];
[_headerRefreshView scrollViewDidScroll];
[_footerRefreshView scrollViewDidScroll];
}
Expand Down Expand Up @@ -743,11 +653,7 @@ - (void)didReceiveMemoryWarning {
}

- (void)cleanUpCachedItems {
//purge view
NSArray<NSNumber *> *objects = [_cachedItems allValues];
[self.renderImpl purgeViewsFromComponentTags:objects onRootTag:self.rootTag];
//purge cache
[_cachedItems removeAllObjects];
// nop
}

@end

0 comments on commit e831f31

Please sign in to comment.