How to notify the background thread from the main thread when there is something needs to be processed

StackOverflow https://stackoverflow.com/questions/12382400

  •  01-07-2021
  •  | 
  •  

Domanda

I am working on an app that does image processing and displays the resulting image. Im using UIScrollView to let user scroll all images, because the image is not a standard jpg or png, it takes time to load. so i want to use a thread to load image, and then update the views.

For now, I use a timer in the background thread to check whether there are any images that are needed to be loaded. but it is not working so well. I want to know whether there is a way to notify the background thread when there are some images that are needed to be loaded in the main thread or any other suggestions?

Thanks in advance.

È stato utile?

Soluzione

Provide a method in your class that controls the scrollView, lets call in 'processImage'. In your background thread, when you have an image, send it to the UI class as follows:

dispatch_async(dispatch_get_main_queue(), ^{ [uiClass processImage:theImage] } );

The background object should keep a weak reference to the uiClass (which is a delegate in this example). The idea is to do the image processing in the background, but provide it to the UI class on the main thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top