Help with a Swift OS X program

John Shockey john at johncshockey.com
Tue Jun 2 07:53:42 EDT 2015


David,

My apologies for not getting back to you more quickly. I started a
response, but I'm working under a deadline and it swallowed me up before
I finished it.

What I was working on was a bit longer, but what Erik said is correct,
and along the same lines as I was writing.

Just because it's important, I'll repeat a part of what he said: You
should never attempt to do synchronous (blocking) programming, and
synchronous network programming in particular, on the main thread.

That leaves you at least three possibilities.

1. Move the networking to an alternate thread. I don't think this will
be the best choice. Among other things, you'd need to use lower level
APIs if you really want to do your networking synchronously. Plus,
synchronizing between threads allows all sorts of things to go wrong.
Sometimes it's worth it to use threads, but it's probably too much work.

2. Write a real command line utility, and a UI program that runs it in a
separate process. There are times when this is a good approach, but it
seems like way too much work in your situation.

3. Do your networking asynchronously. It's fairly simple once you
understand how it works, and there's no need for explicit threading.
(Though Cocoa may use threads behind the scenes, you won't have to deal
with it.) You will need to rearrange how you're doing things, but
learning to do it the was Cocoa intends should be worth the effort.

Again, what Erik said is correct. I'm just a little more long-winded.
<grin>

If you want help figuring out how to use NSURLSession, feel free to
bring your questions to a meeting. We don't have a specific presentation
scheduled this month, so there should be plenty of time. If you can't
make it to a meeting, and if Erik hasn't already helped you
sufficiently, take a look at the documentation for NSURLSession and
friends, and then ask again here.

Good luck,

John



More information about the MacTechGroup-discuss mailing list