UIActivityIndicatorView won't stop

。_饼干妹妹 提交于 2019-12-05 14:42:56

Never never never (never!) talk to the interface on a background thread. What you want is to jump back into the main thread - as you rightly suspect. It's easy-peasy:

[spinner startAnimating];
dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL:
                    kLatestKivaLoansURL];
    // ... do other stuff in the background
    dispatch_async(dispatch_get_main_queue(), ^{
        [spinner stopAnimating];
    });
});

However, your use of performSelectorOnMainThread:withObject:waitUntilDone: with waitUntilDone:YES is also wrong (or at least a very bad smell), since you should not be blocking on your background thread. Just call a method right here on the background thread (unless it touches the interface) and when the result comes back, it comes back.

also call the method removeFromSuperview after stopAnimating

    [spinner stopAnimating];
    [spinner removeFromSuperview];

UIKit calls can only be made in the main thread. Try this:

dispatch_async(kBgQueue, ^{

     NSData* data = [NSData dataWithContentsOfURL:kLatestKivaLoansURL];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                       withObject:data waitUntilDone:YES];
     NSLog(@"loans: %@", spinner);

     dispatch_async(dispatch_get_main_queue(), ^{

         [spinner stopAnimating];

     });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!