Pages

Tuesday, February 5, 2013

Blocking stream operation stopped working on iOS6.1

Background:

My project is a software library that allows its user to access a proprietary 30 pin accessory. The library uses NSInput/OutputStream as part of using the External Accessory framework.

 

The library has to use NSStream in two ways to access the external accessory (These two ways are part of the public API provided by the library, and I would rather not change it if possible):

(1) Asynchronous read (reading data asynchronously coming from the accessory). This is accomplished by scheduling the input stream on a run loop and setting a delegate to handle stream events.

(2) A blocking write, follow by a blocking read with timeout (sending to accessory a command and getting from it a response). This is presented to the library client as a method with signature "(NSData*) sendCommandAndReadResponse:(NSData*) cmd". Blocking IO is used because typically there is negligible IO latency and a blocking IO interface is much easier for client to use.

 

To perform (2), the blocking write-read, the library does the following:

-First, [NSOutputStream write:maxLength:] is used for writing

-Then the input stream delegate is temporarily set to nil to temporarily disable the part of the library that does asynchronous read.

-Then [NSInputStream hasBytesAvailable] and [NSInputStream read:maxLength:] are used for polled-based blocking read with timeout.

-Then input stream delegate is restored

 

Problem:

The blocking write-read worked fine since iOS 4, but has a problem when iOS was updated to 6.1. On iOS 6.1, when performing two blocking write-read in the same UI event, the second write cannot be performed because the output stream reports no space is available.

 

-If only one blocking write-read is performed in a UI event, or, if two blocking write-read are made in different UI event, it still works.

-There does not seem to be changes to NSStream or External Accessory in iOS 6.1 from the class reference

 

Question:

-what could be causing the problem in iOS 6.1?

-is the above method of using both asynchronous write/ synchronous write-read the proper way?

 

Any help is appreciated. Thanks!


View the original article here

0 comments:

Post a Comment