我有一个定制UITableViewCell
的AVPlayer
内容.我添加了观察者,playerItem
以了解视频何时到达终点.该细胞可以得到一个新的文件进行播放,在这种情况下,它取代了player
与playerItem
新的实例.在以前我添加观察者时,我必须在dealloc中删除它,否则应用程序会崩溃.
这一次,我注意到即使我在删除之前没有移除观察者,playerItem
一切正常.
在这种情况下,为什么我不需要删除观察者?
@interface CallResultTableViewCell : UITableViewCell @property (nonatomic, strong) AVPlayer *player; @property (nonatomic, strong) AVPlayerItem *playerItem; @property (nonatomic, strong) NSURL *url; -(void) embedUrl:(NSURL *)url; @end
-(void) embedUrl:(NSURL *)url { if (self.url == nil || ![self.url isEqual:url]) { if (self.player != nil) { [self.player pause]; self.player = nil; //[[NSNotificationCenter defaultCenter] removeObserver:self // name:AVPlayerItemDidPlayToEndTimeNotification // object:self.playerItem]; self.playerItem = nil; //... } if (url != nil) { self.playerItem = [myUrls playerItemWithURL:url]; self.player = [AVPlayer playerWithPlayerItem:self.playerItem]; self.url = url; //... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem]; } } [self.player play]; } -(void)itemDidFinishPlaying:(NSNotification *) notification { [self.playerItem seekToTime:kCMTimeZero]; [self.player play]; }
我还呼吁remove observer
在dealloc
:
-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem]; }
ARC已开启.
来自Apple Developer发行说明:
在OS X 10.11和iOS 9.0中,NSNotificationCenter和NSDistributedNotificationCenter将不再向可能已解除分配的已注册观察者发送通知.
这意味着您无需从iOS 9或OS X 10.11中删除观察者.