Instantiating a MPMusicPlayerController over and over again. Good/Bad Practice
I am not 100% sure what is happening under the hood for the MPMusicPlayerController (Source code would be nice). I don't know how heavy a load I am creating on the app by creating one every time a method like my delegate method handlePlayPauseTapped is called. I would assume because of how you instantiate it, i.e. without alloc/init, that it is doing some caching for me automagically; however, the API docs don't say anything about this.
Should I have a global variable, initiate it once a开发者_JAVA百科nd reuse it for the life of the app? Or, can I save myself some code complexity and just create one everywhere I need one. In my case, I need an iPod Controller from numerous views.
- (IBAction) handlePlayPauseTapped {
MPMusicPlayerController *iPodController = [MPMusicPlayerController iPodMusicPlayer];
if (iPodController.playbackState == MPMusicPlaybackStatePlaying) {
[iPodController pause];
} else {
[iPodController play];
}
}
thoughts on a best practice here?
I suspect that your code isn't actually creating new intances (seems more likely to internally be a single object). This could easily be checked in the debugger, of course.
For the sake of greater flexibility, I would probably make the music player controller an instance variable of my class, which would render the question moot anyway.
精彩评论