天蓬老师2017-04-18 09:26:57
Your question is really abstract
1. Receiving or sending data is an independent function, assuming it is sendRead, separate it from the business. In this way, when receiving data, just receive the data. When the data reaches the standard you want, call back from the sendRead module.
readFromSocket(buffer)
This method can be transformed into a style with a closure (block) callback:
func readFromSocket(data:NSData,completeHander:(receivedData:NSData?)-> Void){
//接收数据的事
//在某个时机调用回调,应该不是当前这个方法里,接收数据应该都是异步的吧
completeHander(receivedData: xxx)
}
When your business execution reaches a certain stage and you need certain data, call this and then process it in the callback. At this time, the callback only needs to handle the logic of this stage. Although this still cannot make sequential calls, because receiving data cannot be completed at once, and it must wait for the data recipient to proactively notify. 2. I see that you have to make multiple status judgments in handleData. Is this business running with multiple threads at the same time? If not, it would be simpler, because the APP is currently in one state, and you only need to collect the data of a single state first and then throw it to the business code for processing. Otherwise, the business logic must be separated, and the read operations must also be separated.