ReactiveObjC
ReactiveObjC copied to clipboard
RAC(2.5) KVO observer crash
my code:
[RACObserve(self.bookShelfView.collectionView, contentOffset) subscribeNext: ^(id value) {
CGFloat offsetY = [value CGPointValue].y;
@strongify(self);
self.headerView.cycleScrollView.contentOffSet = [value CGPointValue];
//Other code
}];
The crash log:
An instance 0x10ca04200 of class UICollectionView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170622500> (,6.0.0.0,222,2017-07-26 15:44:16,479CB4B1-C4F0-3CB9-A3E1-B1A676E4E2FD,arm64,"""[""""An instance 0x10ca04200 of class UICollectionView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170622500> ("""", """"<NSKeyValueObservance 0x17445a430: Observer: 0x17446edc0, Key path: contentOffset, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x17424ced0>"""", """")"""", """"(null)"""", """"(("""", """"\t0 CoreFoundation 0x0000000182b36ff8 + 148"""", """"\t1 libobjc.A.dylib 0x0000000181598538 objc_exception_throw + 56"""", """"\t2 CoreFoundation 0x0000000182b36f28 + 0"""", """"\t3 Foundation 0x0000000183592120 + 300"""", """"\t4 DDReaderPlus 0x100480774 DDReaderPlus + 4720500"""", """"\t5 libobjc.A.dylib 0x0000000181596f00 + 148"""", """"\t6 libobjc.A.dylib 0x00000001815a4334 objc_destructInstance + 92"""", """"\t7 libobjc.A.dylib 0x00000001815a4398 object_dispose + 28"""", """"\t8 UIKit 0x000000018900a470 + 156"""", """"\t9 UIKit 0x0000000188c65c5c + 1636"""", """"\t10 Foundation 0x0000000183592050 + 92"""", """"\t11 DDReaderPlus 0x1004ca9e4 DDReaderPlus + 5024228"""", """"\t12 libobjc.A.dylib 0x0000000181596f00 + 148"""", """"\t13 libobjc.A.dylib 0x00000001815a4334 objc_destructInstance + 92"""", """"\t14 libobjc.A.dylib 0x00000001815a4398 object_dispose + 28"""", """"\t15 UIKit 0x000000018900a470 + 156"""", """"\t16 UIKit 0x0000000188de9244 + 1776"""", """"\t17 DDReaderPlus 0x1007261c4 DDReaderPlus + 7496132"""", """"\t18 DDReaderPlus 0x1004b4698 DDReaderPlus + 4933272"""", """"\t19 DDReaderPlus 0x100844c3c DDReaderPlus + 8670268"""", """"\t20 Foundation 0x0000000183592050 + 92"""", """"\t21 libsystem_blocks.dylib 0x0000000181a43a68 _Block_release + 160"""", """"\t22 DDReaderPlus 0x10038b5e4 DDReaderPlus + 3716580"""", """"\t23 libsystem_blocks.dylib 0x0000000181a43a68 _Block_release + 160"""", """"\t24 libdispatch.dylib 0x00000001819ee9a0 + 16"""", """"\t25 libdispatch.dylib 0x00000001819f35e8 _dispatch_main_queue_callback_4CF + 996"""", """"\t26 CoreFoundation 0x0000000182ae50c8 + 12"""", """"\t27 CoreFoundation 0x0000000182ae2ce4 + 1572"""", """"\t28 CoreFoundation 0x0000000182a12da4 CFRunLoopRunSpecific + 424"""", """"\t29 GraphicsServices 0x000000018447c074 GSEventRunModal + 100"""", """"\t30 UIKit 0x0000000188ccd058 UIApplicationMain + 208"""", """"\t31 DDReaderPlus 0x10020c444 DDReaderPlus + 2147396"""", """"\t32 libdyld.dylib 0x0000000181a2159c + 4"""", """")"""", """""""", """"dSYM UUID: 479CB4B1-C4F0-3CB9-A3E1-B1A676E4E2FD"""", """"CPU Type: arm64"""", """"Slide Address: 0x0000000100000000"""", """"Binary Image: DDReaderPlus"""", """"Base Address: 0x0000000100088000""""]""",-[DDBSView .cxx_destruct] /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/DDReaderPlus/sub-modules/UserCenter/sub-modules/Shelf/View/BSHome/DDBSView.m: line 37,-[BookShelfNewViewController .cxx_destruct] /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/DDReaderPlus/sub-modules/UserCenter/sub-modules/Shelf/Controller/BookShelf/BookShelfNewViewController.m: line 175,-[DDViewController dealloc] /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/Pods/DDReaderBussinessManager/DDReaderBussinessManager/DDBussinessKit/DDController/DDViewController.m: line 29,-[BookShelfNewViewController dealloc] /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/DDReaderPlus/sub-modules/UserCenter/sub-modules/Shelf/Controller/BookShelf/BookShelfNewViewController.m: line 224,__swizzleDeallocIfNeeded_block_invoke /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/Pods/ReactiveCocoa/ReactiveCocoa/NSObject+RACDeallocating.m: line 37,_destroy_helper_block /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/DDReaderPlus/sub-modules/UserCenter/sub-modules/CloudBookShelf/Module/Business/DDCloudBooksManager.m: line 106,main /Users/bruce/Desktop/DDBook/ddreader5/DDReaderPlus/DDReaderPlus/Module/main.mm: line 12
Well, it seems collectionView
is deallocated while the subscription is still active. You can try changing it to the following
[RACObserve(self.bookShelfView, collectionView.contentOffset) subscribeNext: ^(id value) {
CGFloat offsetY = [value CGPointValue].y;
@strongify(self);
self.headerView.cycleScrollView.contentOffSet = [value CGPointValue];
// Other code ...
}];
to observe self.bookShelfView
for changes to the collectionView
property, instead.
But actually UIKit isn't KVO-compliant (even if it usually seems to work), so you probably shouldn't be observing that, anyway.