Data chunking while handling streaming APIs


Hey, trying to implement messaging feature using APIs that stream multiple responses. The responses are JSONs separated by new lines (so called json lines). I'm leveraging URLSessionDataTask and receiving responses in urlSession(_ session:, dataTask:, didReceive data:).

I've noticed that when response is bigger, which happens for first streamed line if there are a lot of conversations or messages, data comes in irregularly sized chunks, which I guess is to be expected from this delegate. My question is wether that chunking behaviour can be somehow prevented, and if not what would be the alternative to avoid having to handle buffering and appending bytes?

Thanks in advance, U.M.

wether that chunking behaviour can be somehow prevented

NO. That chunking is dependent both network environment and implementation details. You cannot make control over them.

if not what would be the alternative to avoid having to handle buffering and appending bytes?

No way. Buffering is the only option if you want to use intermediate results.
You should better not think how to avoid, but better think how to do it better.
Thanks for the reply, I've kinda guessed it was like this. Now I'm wondering if we have a guarantee that these chunks come in the right order, so we can just append them until they become valid JSONs.

I'm wondering if we have a guarantee that these chunks come in the right order, so we can just append them until they become valid JSONs.

The answer is YES.

Please see Apple's documentations:

urlSession(_:dataTask:didReceive:)

Discussion

Because the data object parameter is often pieced together from a number of different data objects, whenever possible, use the enumerateBytes(_:) method to iterate through the data rather than using the bytes method (which flattens the data object into a single memory block).
This delegate method may be called more than once, and each call provides only data received since the previous call. The app is responsible for accumulating this data if needed.
I think the bold face part is stating that the delegate is called in the right order.

If you think it is not clear enough, please see another article:

Fetching Website Data into Memory

Showing an example of implementing urlSession(_:dataTask:didReceive:):
(In Listing 3)

Code Block
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.receivedData?.append(data)
}
And I know many apps assuming the right order are running without any issues.
Data chunking while handling streaming APIs
 
 
Q