仅在尚未运行目标C的情况下如何运行线程

时间:2018-12-18 00:34:33

标签: ios objective-c

所以我有这个方法,当有问题的视图控制器加载时运行。但是,如果启动运行该线程的线程的次数超过应用程序崩溃次数的3倍左右。我需要确保任何时候在线程中仅运行该方法的一个实例,据我所知,这种运行线程的方法没有要检查的线程对象名称,我该怎么做。 >

- (void)observeNowPlaying{
    [self performSelectorInBackground:@selector(changeSongValues) withObject:nil]; //run the song checker in the background
}

-(void)changeSongValues{

    while(threadX != 1){
        MPMediaItem *tempItem = musicController1.nowPlayingItem; //set up now playing
        checkingSong = tempItem.title;
        NSString *artist = tempItem.artist;

        checkingSong = [[NSString alloc]initWithFormat:@"%@ - %@",(artist.length>0 ? artist : @"Unknown"),(checkingSong.length>0 ? checkingSong : @"Unknown")];
        _SongPlaying.text = checkingSong; //this changes the currently playing song and stuff

        usleep(5);
    }
}

0 个答案:

没有答案