user
user

Reputation: 3456

What is the best networking solution for a complex multithreaded app?

I have a streaming iOS app that captures video to Wowza servers. It's a beast, and it's really finicky.

I'm grabbing configuration settings from a php script that shoots out JSON. Now that I've implemented that, I've run into some strange threading issues. My app connects to the host, says its streaming, but never actually sends packets.

Getting rid of the remote configuration NSURLConnection (which I've made sure is properly formatted) delegate fixes the problem. So I'm thinking either some data is getting misconstrued across threads or something like that.

What will help me is knowing:

Upvotes: 2

Views: 144

Answers (4)

Vignesh
Vignesh

Reputation: 10251

Are NSURLConnection delegate methods called on the main thread?

Yes, on request completion it gives a call back on the main thread if you started it on the main thread.

Will nonatomic data be vulnerable in a delegate method?

Generally collection values (like array) are vulnerable with multiple threads; the rest shouldn't create anything other than a race problem.

When dealing with a complex threaded app, what are the best practices for grabbing data from the web?

I feel it's better to use GCD for handling your threads, and asynchronous retrieval using NSURLConnection should be helpful. There are few network libraries available to do the boilerplate code for you, such as AFNetworking, and ASIHTTPRequest (although that is a bit old).

Upvotes: 2

CouchDeveloper
CouchDeveloper

Reputation: 19116

Are NSURLConnection delegate methods called on the main thread?

Delegate methods can be executed on a NSOperationQueue or a thread. If you not explicitly schedule the connection, it will use the thread where it receives the start message. This can be the main thread, but it can also any other secondary thread which shall also have a run loop.

You can set the thread (indirectly) with method

- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode

which sets the run loop which you retrieved from the current thread. A run loop is associated to a thread in a 1:1 relation. That is, in order to set a certain thread where the delegate methods shall be executed, you need to execute on this thread, retrieve the Run Loop from the current thread and send scheduleInRunLoop:forMode: to the connection.

Setting up a dedicated secondary thread requires, that this thread will have a Run Loop. Ensuring this is not always straight forward and requires a "hack".

Alternatively, you can use method

- (void)setDelegateQueue:(NSOperationQueue *)queue

in order to set the queue where the delegate methods will be executed. Which thread will be actually used for executing the delegates is then undetermined.

You must not use both methods - so schedule on a thread OR a queue. Please consult the documentation for more information.

Will nonatomic data be vulnerable in a delegate method?

You should always synchronize access to shared resources - even for integers. On certain multiprocessor systems it is not even guaranteed that accesses to a shared integer is safe. You will have to use memory barriers on both threads in order to guarantee that.

You might utilize serial queues (either NSOperationQueue or dispatch queue) to guarantee safe access to shared resources.

When dealing with a complex threaded app, what are the best practices for grabbing data from the web?

Utilize queues, as mentioned, then you don't have to deal with threads. "Grabbing data" is not only a threading problem ;)

If you prefer a more specific answer you would need to describe your problem in more detail.

Upvotes: 1

savner
savner

Reputation: 840

To answer your first question: The delegate methods are called on the thread that started the asynchronous load operation for the associated NSURLConnection object.

Upvotes: 0

Janene Pappas
Janene Pappas

Reputation: 1356

Have you looked at AFNetworking?

http://www.raywenderlich.com/30445/afnetworking-crash-course

https://github.com/AFNetworking/AFNetworking

It's quite robust and helps immensely with the threading, and there are several good tutorials.

Upvotes: 3

Related Questions