Question

I'm loading data from a server and processing the response asynchronously. Using NSURLConnection. Each call to the delegate, connection:didReceiveData: is processing that chunk.

However, right before a big chunk of data is received, I requested a text view to scroll to a new line, ie [textView scrollRangeToVisible:].

The problem is that often, the textView does not scroll until all the data have been received. This can be a while and makes the UI look hung.

To attempt to work around it, I was thinking the main thread is locked up processing the data, so I should break it up into smaller chunks.

So I changed to code to break up the data received in connection:didReceiveData to perform smaller operations, and queued them up using

[self performSelectorOnMainThread:withObject:smallerChunk waitUntilDone:FALSE]

The thinking being that it puts a bunch of events on the runloop and would handle the scrollRangeToVisible as it could.

However this isn't working. At least not reliably. What am I missing?

Was it helpful?

Solution

"However this isn't working. At least not reliably." is a pretty vague error description. However, I would suggest that you process the data received from the connection in a background thread so that it will not block the main thread.

You can for example create a serial dispatch queue:

dispatch_queue_t myQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);

and in connection:didReceiveData: submit the processing of the data to that queue:

dispatch_async(myQueue, ^{
    /* process data */
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top