Skip to content

Commit 730a99d

Browse files
author
dengjianc
committed
解决ios10下因KVO引起的崩溃 issue #6
解决ios10下因KVO引起的崩溃 issue #6
1 parent 70c1817 commit 730a99d

6 files changed

Lines changed: 34 additions & 18 deletions

File tree

CollectionViewSideRefresh.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
buildSettings = {
371371
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
372372
CODE_SIGN_STYLE = Automatic;
373-
DEVELOPMENT_TEAM = DTCHC66PTA;
373+
DEVELOPMENT_TEAM = 4QA4S2484R;
374374
INFOPLIST_FILE = CollectionViewSideRefresh/Info.plist;
375375
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
376376
LD_RUNPATH_SEARCH_PATHS = (
@@ -388,7 +388,7 @@
388388
buildSettings = {
389389
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
390390
CODE_SIGN_STYLE = Automatic;
391-
DEVELOPMENT_TEAM = DTCHC66PTA;
391+
DEVELOPMENT_TEAM = 4QA4S2484R;
392392
INFOPLIST_FILE = CollectionViewSideRefresh/Info.plist;
393393
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
394394
LD_RUNPATH_SEARCH_PATHS = (

CollectionViewSideRefresh/SideRefresh/SideRefresh.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ @implementation SideRefresh
1313

1414
- (void)willMoveToSuperview:(UIView *)newSuperview {
1515
[super willMoveToSuperview:newSuperview];
16+
[self removeObserver];
1617
if ([newSuperview isKindOfClass:[UICollectionView class]]) {
1718
self.collectionView = (UICollectionView *)newSuperview;
1819
self.collectionView.alwaysBounceHorizontal = YES;
1920
self.originalContentInset = self.collectionView.side_inset;
2021
self.collectionViewPageEnabel = self.collectionView.pagingEnabled;
21-
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
22-
[self.collectionView addObserver:self forKeyPath:@"contentOffset" options:options context:nil];
23-
[self.collectionView addObserver:self forKeyPath:@"contentSize" options:options context:nil];
22+
[self addObserver];
2423
}
2524
}
2625

@@ -38,9 +37,15 @@ - (void)endLoading {
3837
self.refreshStatus = SideRefreshStatusNormal;
3938
}
4039

41-
- (void)dealloc {
42-
[self.collectionView removeObserver:self forKeyPath:@"contentOffset"];
43-
[self.collectionView removeObserver:self forKeyPath:@"contentSize"];
40+
- (void)removeObserver {
41+
[self.superview removeObserver:self forKeyPath:@"contentOffset"];
42+
[self.superview removeObserver:self forKeyPath:@"contentSize"];
43+
}
44+
45+
- (void)addObserver {
46+
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
47+
[self.collectionView addObserver:self forKeyPath:@"contentOffset" options:options context:nil];
48+
[self.collectionView addObserver:self forKeyPath:@"contentSize" options:options context:nil];
4449
}
4550

4651
#pragma mark - ContentOffset Observer

CollectionViewSideRefresh/SideRefresh/SideRefreshEmptyFooter.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
2828

2929
- (void)willMoveToSuperview:(UIView *)newSuperview {
3030
[super willMoveToSuperview:newSuperview];
31+
[self.superview removeObserver:self forKeyPath:@"contentSize"];
3132
if([newSuperview isKindOfClass:[UICollectionView class]]) {
3233
self.collectionView = (UICollectionView *)newSuperview;
3334
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
@@ -52,10 +53,6 @@ - (void)setupMessageLabel {
5253
[self addSubview:_messageLabel];
5354
}
5455

55-
- (void)dealloc {
56-
[self.collectionView removeObserver:self forKeyPath:@"contentSize"];
57-
}
58-
5956
#pragma mark - ContentOffset Observer
6057
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
6158
if([keyPath isEqualToString:@"contentSize"]) {

CollectionViewSideRefresh/SideRefreshDemo/CustomViewController.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ - (void)viewDidLoad {
2121
[super viewDidLoad];
2222
// Do any additional setup after loading the view, typically from a nib.
2323
self.title = @"自定义";
24-
self.itemCount = 3;
24+
self.itemCount = 0;
2525

2626
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
2727
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
@@ -34,11 +34,15 @@ - (void)viewDidLoad {
3434

3535
[self setupRefreshHeaderFoorer];
3636
}
37+
- (void)viewDidAppear:(BOOL)animated {
38+
[super viewDidAppear:animated];
39+
[self.collectionView.sideRefreshHeader beginLoading];
40+
}
3741

3842
- (void)setupRefreshHeaderFoorer {
3943
__weak typeof(self) weakSelf = self;
4044
SideRefreshHeader *refreshHeader = [SideRefreshHeader refreshWithLoadAction:^{
41-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
45+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
4246
__strong __typeof(self) strongSelf = weakSelf;
4347
strongSelf.itemCount = 3;
4448
[strongSelf.collectionView.sideRefreshHeader endLoading];

CollectionViewSideRefresh/SideRefreshDemo/EmptyFooterViewController.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (void)viewDidLoad {
2222
[super viewDidLoad];
2323
// Do any additional setup after loading the view, typically from a nib.
2424
self.title = @"没有更多数据-提示";
25-
self.itemCount = 3;
25+
self.itemCount = 0;
2626

2727
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
2828
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
@@ -37,10 +37,15 @@ - (void)viewDidLoad {
3737
[self setupRefreshHeaderFoorer];
3838
}
3939

40+
- (void)viewDidAppear:(BOOL)animated {
41+
[super viewDidAppear:animated];
42+
[self.collectionView.sideRefreshHeader beginLoading];
43+
}
44+
4045
- (void)setupRefreshHeaderFoorer {
4146
__weak typeof(self) weakSelf = self;
4247
SideRefreshHeader *refreshHeader = [SideRefreshHeader refreshWithLoadAction:^{
43-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
48+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
4449
__strong __typeof(self) strongSelf = weakSelf;
4550
strongSelf.itemCount = 3;
4651
[strongSelf.collectionView.sideRefreshHeader endLoading];

CollectionViewSideRefresh/SideRefreshDemo/HideItemViewController.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (void)viewDidLoad {
2222
[super viewDidLoad];
2323
// Do any additional setup after loading the view, typically from a nib.
2424
self.title = @"隐藏提示或动画";
25-
self.itemCount = 3;
25+
self.itemCount = 0;
2626

2727
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
2828
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
@@ -37,10 +37,15 @@ - (void)viewDidLoad {
3737
[self setupRefreshHeaderFoorer];
3838
}
3939

40+
- (void)viewDidAppear:(BOOL)animated {
41+
[super viewDidAppear:animated];
42+
[self.collectionView.sideRefreshHeader beginLoading];
43+
}
44+
4045
- (void)setupRefreshHeaderFoorer {
4146
__weak typeof(self) weakSelf = self;
4247
SideRefreshHeader *refreshHeader = [SideRefreshHeader refreshWithLoadAction:^{
43-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
48+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
4449
__strong __typeof(self) strongSelf = weakSelf;
4550
strongSelf.itemCount = 3;
4651
[strongSelf.collectionView.sideRefreshHeader endLoading];

0 commit comments

Comments
 (0)