开发者

iPhone: Screen not redrawing when I do something heavy _afterwards_

I want to post a message and a picture to twitter (using a picture hoster). If the user enters a message alone and then taps send my overlay appears and shows "Sending message to twitter". If the user adds a picture it doesn't show that overlay. Maybe because he has to convert it to jpg and thats heavy load? My code looks like this:

[overlayMessage changeTextTo:NSLocalizedString(@"keySendTwitter", nil)];
overlayMessage.hidden = NO; 
overlayMessage.locked = YES;

// I've added these out of desperation :P
[overlayMessage setNeedsDisplay];
[self.view setNeedsDisplay];

[self sendMessage];

It seems like the 开发者_开发百科two messages (changeTextTo: and sendMessage:) are running in parallel and that sendMessage is a little bit faster with doing the heavy loading. But that would require threading and my app is not multithreaded.

Or maybe setNeedsDisplay: is the wrong message to update the screen before the heavy loading?


Not 100% clear what you're doing, but it sounds like your [self sendMessage] method is blocking the UI from updating. Which makes sense if you're doing a large synchronous network operation or image conversion. You don't have to mess around too much with threading to get the UI to be responsive during heavy operations. simply call sendMessage on a different thread with:

[self performSelector@selector(sendMessage) withObject:nil afterDelay:0.001f];

or

[self performSelectorInBackground:@selector(sendMessage) withObject:nil];

Your method will end, your UI will update (no need for setNeedsDisplay, probably) and your sendMessage selector will fire. You are in charge of making sure sendMessage causes your loading screen to be dismissed, but you'll think of something for that :)

The key idea here is that interface updates happen in batches. A bit of your code runs, and then the interface updates... another bit of your code runs, and the interface updates.

What's happening here is that your code is too bulky, and the ui can't update until it's done. Calling setNeedsDisplay only marks a view as "dirty" and in need of redrawing, it doesn't actually re-draw the view.

I suggest either break your operation into asynchronous chunks, use NSOperationQueue, or use one of the performSelector variants.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜