Can a URLSessionStreamTask be used for both reading and writing?

248 views Asked by At

In the startLoading method of my URLProtocol subclass, I create a URLSession and URLSessionStreamTask. I resume the task and add a BlockOperation to call my first transaction method. That method uses URLSessionStreamTask.write. If I don't get an error, another BlockOperation is placed with my second transaction method. That method uses URLSessionStreamTask.read and it always times out.

The error looked like:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x10945dff0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=gopher://www.floodgap.com:80/GET%20/, NSErrorFailingURLKey=gopher://www.floodgap.com:80/GET%20/, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

Can a stream-task only be used for one I/O call? Or only just writing or just reading? Or is there some mode-switch function I forgot to call between the write and read? Could there be another problem?

1

There are 1 answers

0
Kai On

Try to add read block operation before write to the streamTask. Read and write are asynchron calls, waiting until timeout happens. In your example you send a command with write, I guess it receives and answer very quick, before adding read to the task is done. So you never see your response.