0

全部,

我试图在加载 UIViewController 时异步加载一组声音。大约在同一时间,我(偶尔)还会在我的 ViewController 层次结构的顶部放置一个 UIView 以呈现帮助覆盖。当我这样做时,应用程序会因执行错误而崩溃。如果未添加视图,应用程序不会崩溃。我的 ViewController 看起来像这样:

- (void)viewDidLoad
{
    [super viewDidLoad];

    __soundHelper = [[SoundHelper alloc] initWithSounds];

    // Other stuff
}

- (void)viewDidAppear:(BOOL)animated
    {
    // ****** Set up the Help Screen
    self.coachMarkView = [[FHSCoachMarkView alloc] initWithImageName:@"help_GradingVC" 
                                                    coveringView:self.view 
                                                     withOpacity:0.9 
                                                    dismissOnTap:YES 
                                                    withDelegate:self];

    [self.coachMarkView showCoachMarkView];
    [super viewDidAppear:animated];
}

(从'initWithSounds'调用)的主要异步加载方法SoundHelper如下所示:

// Helper method that loads sounds as needed
- (void)loadSounds {

    // Run this loading code in a separate thread
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    NSBlockOperation *loadSoundsOp = [NSBlockOperation blockOperationWithBlock:^{
        // Find all sound files (*.caf) in resource bundles
        __soundCache = [[NSMutableDictionary alloc]initWithCapacity:0];

        NSString * sndFileName;
        NSArray *soundFiles = [[NSBundle mainBundle] pathsForResourcesOfType:STR_SOUND_EXT inDirectory:nil];

        // Loop through all of the sounds found
        for (NSString * soundFileNamePath in soundFiles) {
            // Add the sound file to the dictionary
            sndFileName = [[soundFileNamePath lastPathComponent] lowercaseString];
            [__soundCache setObject:[self soundPath:soundFileNamePath] forKey:sndFileName];
        }

        // From: https://stackoverflow.com/questions/7334647/nsoperationqueue-and-uitableview-release-is-crashing-my-app
        [self performSelectorOnMainThread:@selector(description) withObject:nil waitUntilDone:NO];
    }];
    [operationQueue addOperation:loadSoundsOp];
}

当块退出时,崩溃似乎发生了。的看起来像这样initFHSCoachMarkView

- (FHSCoachMarkView *)initWithImageName:(NSString *) imageName 
                           coveringView:(UIView *) view
                            withOpacity:(CGFloat) opacity
                           dismissOnTap:(BOOL) dismissOnTap
                           withDelegate:(id<FHSCoachMarkViewDelegate>) delegateID
{
    // Reset Viewed Coach Marks if User Setting is set to show them
    [self resetSettings];

    __coveringView = view;        
    self = [super initWithFrame:__coveringView.frame];
    if (self) {
        // Record the string for later reference
        __coachMarkName = [NSString stringWithString:imageName];
        self.delegate = delegateID;

        UIImage * image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];

        // ****** Configure the View Hierarchy
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];

        [self addSubview:imgView];
        [__coveringView.superview insertSubview:self aboveSubview:__coveringView];

        // ****** Configure the View Hierarchy with the proper opacity
        __coachMarkViewOpacity = opacity;
        self.hidden = YES;
        self.opaque = NO;
        self.alpha = __coachMarkViewOpacity;

        imgView.hidden = NO;
        imgView.opaque = NO;
        imgView.alpha = __coachMarkViewOpacity;

        // ****** Configure whether the coachMark can be dismissed when it's body is tapped
        __dismissOnTap = dismissOnTap;

        // If it is dismissable, set up a gesture recognizer
        if (__dismissOnTap) {
            UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                      action:@selector(coachMarkWasTapped:)];
            [self addGestureRecognizer:tapGesture];
        }
    }
    return self;
}

我尝试使用两者来调用异步块NSBlockOperationdispatch_async并且两者都具有相同的结果。此外,我完全删除了 aysnch 调用并将声音加载到主线程上。这很好用。我还尝试了@Jason 在中建议的解决方案:NSOperationQueue 和 UITableView 版本正在使我的应用程序崩溃,但同样的事情也发生在那里。

这实际上是在 FHSCoachMarkView 中添加的视图的问题,还是可能与两者都访问的事实有关mainBundle?我对 iOS 中的异步编码有点陌生,所以我有点茫然。任何帮助,将不胜感激!

谢谢,斯科特

4

1 回答 1

0

我想通了:我在对象SoundHelper(也在NSUserDefaultsDidChangeNotification对. 在 中,我没有正确检查正在更改哪些默认值,因此每次进行更改时都会调用异步声音加载方法。所以多个线程试图修改实例变量。它似乎不喜欢那样。FHSCoachMarkViewNSUserDefaultsSoundHelper__soundCache

问题:这是回答您自己问题的正确方法吗?还是我应该自己对问题添加评论?

谢谢。

于 2012-02-20T02:16:55.223 回答