简体   繁体   中英

Can a URLSessionStreamTask be used for both reading and writing?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM