沒有優化以前的代碼以下性能
[strongSelf.tableView.mj_footer endRefreshing];
[strongSelf.articleArr addObjectsFromArray:feedList];
[strongSelf.tableView reloadData];
優化以後的代碼優化
NSMutableArray *indexPaths = [NSMutableArray array]; [feedList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(strongSelf.articleArr.count + idx) inSection:0]; [indexPaths addObject:indexPath]; }]; [strongSelf.tableView.mj_footer endRefreshing]; [strongSelf.articleArr addObjectsFromArray:feedList]; [strongSelf.tableView beginUpdates]; [strongSelf.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; [strongSelf.tableView endUpdates];
沒有優化以前的代碼以下:spa
[strongSelf.feedList addObjectsFromArray:feedList]; if (feedList.count < kPageSize) { [strongSelf.collectionView.mj_footer endRefreshingWithNoMoreData]; }else{ [strongSelf.collectionView.mj_footer resetNoMoreData]; } [strongSelf.collectionView reloadData];
優化以後的代碼code
NSMutableArray *indexPaths = [NSMutableArray array]; [feedList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [indexPaths addObject:[NSIndexPath indexPathForItem:(strongSelf.feedList.count + idx) inSection:0]]; }]; [strongSelf.feedList addObjectsFromArray:feedList]; if (feedList.count < kPageSize) { [strongSelf.collectionView.mj_footer endRefreshingWithNoMoreData]; }else{ [strongSelf.collectionView.mj_footer resetNoMoreData]; } [strongSelf.collectionView insertItemsAtIndexPaths:indexPaths];
總結:相比較之下,優化以後看似代碼量增長了少量,可是從理論上分頁加載的性能更好了。以前分頁加載使用的全局刷新,優化以後改用了局部刷新。從而性能獲得提高。blog