开发者

When are a methods GUI operations actually carried out?

开发者 https://www.devze.com 2023-02-18 00:02 出处:网络
I am working on a web-services data processing app and I am trying to make the app run as quickly as possible. When a certain 3 finger pan gesture is performed, I call a method that sends updated info

I am working on a web-services data processing app and I am trying to make the app run as quickly as possible. When a certain 3 finger pan gesture is performed, I call a method that sends updated information off to the server to get a new batch of images to update the existing ones with.

So lets say there are 15 images in an array, I filter through them with a 2 finger gesture, and then if I want to change something about them, I can do the 3 finger gesture, and I get that same set back, just tweaked a bit (contrast/brightness, etc.).

Is what I want though is to be able to update the imageView that is displaying the images after the first image has been retrieved, so as to give the user a feel for what the rest in the series are going to look like. But no matter what I try, and no matter how many different threads I try and implement, I can't get the imageView to update before the entire download is complete. Once the batch download is done (which is handled on a separate thread) the imageView updates with the new images and everything is great.

The first step in the process is this:

开发者_Python百科
if(UIGestureRecognizerStateEnded == [recognize state]){
    [self preDownload:windowCounter Level:levelCounter ForPane:tagNumber];// Where this method is what gets the first image, and tries to set it to the imageView
    [self downloadAllImagesWithWL:windowCounter Level:levelCounter ForPane:tagNumber]; //And this method goes and gets all the rest of the images
}

This is my preDownload method:

-(void)preDownload:(int)window Level:(int)level ForPane:(int) pane{

int guidIndex = [[globalGuids objectAtIndex:pane] intValue];

UIImage *img = [DATA_CONNECTION getImageWithSeriesGUID:[guids objectAtIndex:guidIndex] ImageID:counter Window:window Level:level];

if(pane==0){
    NSLog(@"0");
    [imageView3 setImage:img];
}else if(pane==1){
    NSLog(@"1");
    [imageView31 setImage:img];
}else if(pane==2){
    NSLog(@"2");
    [imageView32 setImage:img];
}else if(pane==3){
    NSLog(@"3");
    [imageView33 setImage:img];
}



   }

So by separating this out into two different methods (there are no threads being implemented at this point, these methods are being called before all that) I was thinking that after the preDownload method completed, that the imageView would update, and then control would continue on down into the downloadAllImagesWithWL method, but that doesn't appear to be the case.

Am I missing something simple here? What can I do to update my GUI elements before that second method is through running?


You are right. However the viewn won't refresh until your code reaches runloop. You can do 2 things:

  • Make your downloadAllImagesWithWL method async, so it will return after you called it, your main thread reaches runloop, gui updates, and the download method will tell your logic through a callback when its done.

OR

  • A simplier hackier (and bad) solution would be to run runloop for some time before you call your download method. Something like this: [[NSRunloop currentRunLoop] runUnitlDate: [Date dateWithTimeIntervalSinceNow: 0.1]]; It will run runloop for 0.1 second.


When the image is set, the image view will mark itself as needing display. The actual display won't occur until the beginning of the next run loop. In OS X, you can use -display to draw the view immediately, but I don't think Apple created a public method to do this on iOS. However, if the next method simply creates the background thread, then it will return quickly and the display update will probably occur before the thread finishes.

0

精彩评论

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