Re: Threads
Re: Threads
- Subject: Re: Threads
- From: Graham Cox <email@hidden>
- Date: Sat, 20 Jul 2013 10:57:25 +1000
It *sounds* like what you could be after is asynchronous loading of the image data, so that the cell content is displayed once the image is loaded. That is not simply a matter of calling -setNeedsDisplay asynchronously, which will not achieve anything.
Doing this isn't hard, and it's usually worthwhile to keep your UI responsive if it has to load and decode images from disk or network.
The actual display of the cell must be done on the main thread.
There are several ways to handle this, but here is the outline of it: when the cell needs its image, it calls on some method of the controller to provide it. That method is used to either a) kick off the image load on a background thread, and immediately returns nil, indicating that the image isn't available, or b) returns the image if it has it already. When the background thread has loaded the image, the controller is now supplied the image which it stores in some way that can be associated with the given cell. The controller then calls -setNeedsDisplay for the cell in question. The cell once again asks the controller for the image, and this time it gets it straight away and displays it. Given that outline, the detail implementation can be designed based on how your cell and controller communicate. I usually find KVO to be an easy way to signal between the various parts, but you could use notifications or a dedicated API.
--Graham
On 20/07/2013, at 6:09 AM, koko <email@hidden> wrote:
>
> On Jul 19, 2013, at 1:48 PM, Fritz Anderson <email@hidden> wrote:
>
>> Use an NSOperation, or -performSelectorOnMainThread: (you'll have to wrap the call in your own method), or dispatch_[a]sync() to do it on the main thread.
>
> So are you saying that in the following code:
>
> - (void)setNeedsDisplayWrapper:(BOOL)value
> {
> dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
> {
> [self setNeedsDisplay:value];
>
> dispatch_async(dispatch_get_main_queue(), ^
> {
> NSLog(@"completion");
> });
> });
> }
>
>
> All calls made by Cocoa to display the view will be done a separate thread?
>
>
> -koko
_______________________________________________
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