Re: Concurrent loading of images ?
Re: Concurrent loading of images ?
- Subject: Re: Concurrent loading of images ?
- From: Alex Zavatone via Cocoa-dev <email@hidden>
- Date: Fri, 8 May 2020 17:15:18 -0500
Using a queue is part of GCD. You should probably not use the main queue since
that is analogous to the main thread and the main thread is meant for updating
the UI.
The good thing here is that you can sort of preload the next image in a queue
after your last one has displayed.
With a dispatchQueue, the idea here is that you have lots of tasks that you
want to get done and you want the previous task to complete, then start the
next one and so on and so on. You probably don’t need to do that for this
application.
If I were doing this in Objective-C, I’d fire off a “preload” on an async
dispatch to a block. Something like this.
// create a queue to load the image
dispatch_group_t d_group = dispatch_group_create();
dispatch_queue_t bg_queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(d_group, bg_queue, ^{
NSLog(@“preloading");
Do that image loading here
});
If you did want to do something on the main queue, you can do it like this.
dispatch_async(dispatch_get_main_queue(), ^{
Do your main thread updates here
}
If you want to use a serial queue that doesn’t block the main thread you can do
it like this.
// Dispatch the rest of session setup to the sessionQueue so that the main
queue isn't blocked.
dispatch_queue_t sessionQueue = dispatch_queue_create("session queue",
DISPATCH_QUEUE_SERIAL);
[self setSessionQueue:sessionQueue];
dispatch_async(sessionQueue, ^{
Do stuff here, but you want to read up on dipatch queues to see the
best approach.
}
There’s lots of fun that you can have using these queues and GCD. The trick is
finding the approaches that suit your needs.
Hope these help.
> On May 8, 2020, at 4:53 PM, Gabriel Zachmann via Cocoa-dev
> <email@hidden> wrote:
>
>> I second the use of GCD. Its also considerably simpler than NSThread,
>> NSOperationQueue/NSOperation et al. This is the kind of operation that GCD
>> was invented for.
>
> Thanks a lot for your response(s).
>
> To me a queue suggests that you have lots and lots of tasks arriving from the
> producer , which you need to buffer in queue, so that the consumer can work
> on it one task at a time.
>
> But in my case, I really have just one task, so the queue would - at most -
> contain one task at a time.
> So, why not start the task right away using NSThread or NSOperation?
>
>
> _______________________________________________
>
> 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
_______________________________________________
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