Re: please help with this handler issue
Re: please help with this handler issue
- Subject: Re: please help with this handler issue
- From: Graham Cox <email@hidden>
- Date: Thu, 29 Aug 2013 08:51:15 +0200
On 29/08/2013, at 5:18 AM, Rick C. <email@hidden> wrote:
> But this is an unreliable way to do this I know because I have problems. Can someone please help me with the right way? Because I need for it to wait before advancing or else it will cause problems in following code. Really appreciate the time thanks!
You should state what your goals are and why it's not meeting them. I'm guessing what you're trying to do is to download an image from a server which takes time, but your program can't do anything until you have it.
You need to design your app for asynchronous work like this - just spinning the run loop 'inline' is no good.
As a general solution, I usually follow this outline:
1. Design an object that is responsible for supplying the data you need (image, whatever).
2. Allow the above object to return nil to its client (caller) if the data is not yet available, but use that call to trigger the asynchronous fetch. That fetch can be performed using any of the asynchronous techniques available - GCD, threads, runloop queues, etc.
3. The client must be ready to deal with the nil and defer the task that needs the data until later, returning to the normal run loop while it waits, or get on with something else.
4. When the fetcher has the data, it notifies the client that the data is ready, so that the client can ask for it again, this time with a positive result.
5. The notification in step 4 can use any reasonable method - e.g. an actual notification, an explicit callback to the client, KVO, etc.
The fetcher object wraps up all the functionality relating to the actual fetch and caches the result data so it can return it when required. This general outline works whether the data to be fetched comes from the web, off disk, wherever you need the client to remain responsive while a lengthy background load takes place.
The client code looks like this:
- (void) getData
{
// call initially when you require something
myData = [fetcher getData];
if( myData )
[self processData];
}
- (void) fetchNotification
{
// called by fetcher when it finishes
[self getData];
}
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden