在解雇我的VC后,我注意到我没有从记忆中释放所有东西。关于如何找到保留周期,我很遗憾。我正在使用NSTimer和NSNotificationCenter,但我确保在退出之前使invalid和removeObservers无效,并且我确保使用弱委托。
我的保留周期还能在哪里发生?在动画块?喜欢这个?
[UIView animateWithDuration:.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.setListTableViewVertConst.constant = 0;
self.setListTableViewHeightConst.constant = 264;
} completion:^(BOOL finished) {
}];
使用GCD时,请务必使用weakSelf。
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.remotePasswordTextField resignFirstResponder];
});
感谢您的帮助。
编辑:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Send the room code to be displayed on the respective view controllers.
if ([segue.identifier isEqualToString:@"toSetListRoomVC"]) {
SetListRoomViewController *setListVC = segue.destinationViewController;
setListVC.roomCode = self.roomCodeTextField.text;
}
}
viewWIllApear
[super viewWillAppear:YES];
self.socket = [[SocketKeeperSingleton sharedInstance]socket];
self.socketID = [[SocketKeeperSingleton sharedInstance]socketID];
NSString *roomCodeAsHost = [[SocketKeeperSingleton sharedInstance]hostRoomCode];
/////////HOST/////////
if ([[SocketKeeperSingleton sharedInstance]isHost]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveHostSongAddedNotification:)
name:kQueueAdd
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveUserJoinedNotification:)
name:kUserJoined
object:nil];
NSLog(@"User is the host of this room");
self.isHost = YES;
[self viewForNoCurrentArtistAsHost];
self.roomCodeLabel.text = roomCodeAsHost;
if (!self.hostQueue) {
self.hostQueue = [[NSMutableArray alloc]init];
}
if (!self.hostCurrentArtist) {
self.hostCurrentArtist = [[NSMutableDictionary alloc]init];
}
if (!self.player) {
self.player = [[AVPlayer alloc]init];
}
if (!self.timer) {
self.timer = [[NSTimer alloc]init];
}
}
///////NOT HOST///////
else {
// Add a notifcation observer and postNotification name for updating the tracks.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveQueueUpdatedNotification:)
name:kQueueUpdated
object:nil];
//Add a notifcation observer and postNotification name for updating current artist.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveCurrentArtistUpdateNotification:)
name:kCurrentArtistUpdate
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveOnDisconnectNotification:)
name:kOnDisconnect
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveHostDisconnectNotification:)
name:kHostDisconnect
object:nil];
//Add some animations upon load up. Purple glow and tableview animation.
double delay = .4;
[self purpleGlowAnimationFromBottomWithDelay:&delay];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
//Set Current artist, if there is one.
NSDictionary *currentArtist = [[SocketKeeperSingleton sharedInstance]currentArtist];
[self setCurrentArtistFromCurrentArtist:currentArtist];
//Set the current tracks, if there is one.
NSArray *setListTracks = [[SocketKeeperSingleton sharedInstance]setListTracks];
if (setListTracks) {
self.tracks = setListTracks;
}
}
}
tableVIEWs
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 1) {
if (self.isHost) {
return [self.hostQueue count];
}
else return [self.tracks count];
}
else return [self.searchTracks count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
答案 0 :(得分:1)
我注意到你在一些评论中使用了“popOut”这个词,所以我假设你使用的是导航控制器。
如果是这种情况,您的视图控制器将被嵌入的导航控制器保留,并且不会被释放。导航控制器需要保存对VC的引用(请参阅UINavigationController.viewControllers),以便在弹出层次结构中的上/下VC时可以返回它。
这是预期的行为。
答案 1 :(得分:0)
在此代码示例中,您没有将__weak
用于self
[UIView animateWithDuration:.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.setListTableViewVertConst.constant = 0;
self.setListTableViewHeightConst.constant = 264;
} completion:^(BOOL finished) {
}];
然而,一眼就看出这似乎没有创造一个保留周期。您发布的代码似乎没有,所以它可能在其他地方。你确定你有保留周期吗?
无论如何,请尝试使用instrumenting app。这可能有所帮助。