On May 17, 2014, at 2:16 AM, Charles Carver wrote:

> Thanks for the tips! As I’m new to Cocoa, they’re all very helpful.

You're welcome.  I'm glad to help.


>> You should consider what happens if the server is slow to respond.  The 
>> above call will block until the image download is complete.  Better to use 
>> the asynchronous URL loading mechanisms in the frameworks (e.g. 
>> NSURLSession).
> 
> While working on a similar image download method for a different part of my 
> app, I also encountered the blocking nature of writeToURL. I Googled for a 
> solution (not wanting to refactor my saving with NSURLSession), and found an 
> SO question 
> (http://stackoverflow.com/questions/16283652/understanding-dispatch-async) on 
> dispatch_async.
> 
> Per the top answer, I implemented the following...
> 
> dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 
> 0), ^(void){
> ...
> });
> 
> …to handle the synchronistic nature of my file download/saving.
> 
> Is this frowned upon?

Yes, somewhat.  It can be a quick-and-dirty solution, but it is a bit dirty.  
In particular, the thread that services that task will end up blocked in the 
synchronous URL download operation.  A thread is a fairly expensive resource 
and it's being largely wasted.  It's doing little but waiting.

> Would it be more beneficial in the long run to use NSURLSession instead?

Yes.

Also, GCD has its own share of gotchas.  So, even if you use it, it's not 
necessarily as simple as just wrapping some code in a dispatch_async().  In 
particular, you need to make sure that any GUI-related code is shunted back to 
the main thread.  In your case, the NSSharingService stuff probably qualifies.

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to