以下を追加した後、View Controller の割り当てが解除されなくなりました。
@property (strong, nonatomic) GKLocalPlayer *player;
(in viewDidLoad)
self.player = nil;
[self authenticatePlayer];
- (void)authenticatePlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
__unsafe_unretained typeof(*localPlayer) *blockLocalPlayer = localPlayer;
localPlayer.authenticateHandler =
^(UIViewController *authenticateViewController, NSError *error)
{
if (authenticateViewController != nil)
{
[self presentViewController:authenticateViewController animated:YES
completion:nil];
}
else if (blockLocalPlayer.isAuthenticated)
{
self.player = blockLocalPlayer;
[self openGame];
}
else
{
// Disable Game Center
self.player = nil;
[self openGame];
}
};
}
- (void)setPlayer:(GKLocalPlayer *)player
{
_player = player;
NSString *playerName;
if (_player)
{
playerName = _player.alias;
}
else
{
playerName = @"Anonymous Player";
}
NSLog(@"%@", [NSString stringWithFormat:@"Welcome %@", playerName]);
}
この問題は、ユーザーがゲーム センターに接続するかどうかに関係なく発生します。View Controllerが閉じられた後もメモリに残る原因となっているコードがコードにあるに違いありません。これらの行をコメントアウトすると:
self.player = nil;
[self authenticatePlayer];
ビューコントローラーは、閉じられると適切に割り当て解除されます。
編集:私の勘は正しかった。Apple ドキュメントから:
Game Kit は、ローカル プレーヤーの認証に成功した後でも、完了ハンドラーへの強力な参照を維持します。ゲームがバックグラウンドに移動した場合、Game Kit は、ゲームがフォアグラウンドに戻るたびにプレーヤーを自動的に再認証します。Game Kit は、ローカル プレーヤーを認証するたびに、同じ完了ハンドラーを呼び出します。ブロック プログラミングでは、ブロック内で参照されるすべての Objective-C オブジェクトも、ブロックが解放されるまでブロックによって強く参照されることに注意してください。Game Kit はゲームが終了するまで完了ハンドラーへの強力な参照を維持するため、認証ハンドラー内から参照されるオブジェクトも無期限に保持されます。
しかし、これは私にとって問題です。私は Cocos2d を使用していますが、View Controller の割り当てを完全に解除して新しく作成しない限り、ビューのリセットに問題があります。
Game Kit でビュー コントローラーを解放する方法はありますか?