Andrew_L
Andrew_L

Reputation: 3031

UIActivityIndicatorView in UITableViewCell does not stop spinning

I have a subclassed UITableViewCell named AccountView which contains an UIActivityIndicatorView. Each cell is supposed to contain a Twitter account. When the user posts a message to Twitter, the UIActivityIndicatorView is supposed to start spinning, then stop animating when the twitter request handler is executed:

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
        if (error != nil) {
            NSLog(@"TWITTER ERROR: %@",error);
        }
        NSLog(@"Posted successfully");
        AccountCell *cell = (AccountCell *)[accountTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[accounts indexOfObject:account] inSection:0]];
        NSLog(@"Cell isSpinning?  :%d",[[cell activity] isAnimating]);
        [[cell activity] stopAnimating];
        NSLog(@"Cell isSpinning?  :%d",[[cell activity] isAnimating]);
    }];

This all works fine when posting a short message to Twitter, however, when the TWRequest contains an image to be uploaded, after a few seconds, the post with image appears on Twitter, and I see the following message in the console:

2011-12-10 00:13:22.085 TwitTest[14954:1a03] Posted Successfully
2011-12-10 00:13:22.086 TwitTest[14954:1a03] Cell isSpinning?  :1
2011-12-10 00:13:22.088 TwitTest[14954:1a03] Cell isSpinning?  :0

But the UIActivityIndicatorView keeps spinning indefinitely (or at least a delay of several seconds), even though the Log messages indicate otherwise. The fact that this only breaks with image upload leads me to believe that maybe I should send the request on a background thread, but I have never dealt with threads before, and couldn't say for sure if that is the problem.

One last thing, if I navigate to different view and return, the UIActivityIndicatorView appears frozen - if I navigate away and return a second time, then it disappears for good.

Upvotes: 3

Views: 1040

Answers (2)

Obliquely
Obliquely

Reputation: 7072

You could try adding the line:

[CATransaction flush];

after you've sent the stopAnimating message. This will help if the issue is that the event loop isn't coming around quickly enough. Not clear that this is the issue from what you provide, but worth a try. See this question and answer for background.

Updated you'll need to add #import <QuartzCore/QuartzCore.h> in the header file and make sure you've added the QuartzCore framework using the Build Phases (Link Binary With Libraries) option in Targets.

Upvotes: 1

Gabriel
Gabriel

Reputation: 3359

From Apple Developer's documentation: "This handler is not guaranteed to be called on any particular thread." Perhaps your interface loop is working and modifications are taken in account after each run loop.

Try to execute interface changes in main thread. Use performSelectorOnMainThread:withObject:

Upvotes: 1

Related Questions