search

Home  >  Q&A  >  body text

socket - ios 代理模式如何转换成顺序执行的流程?

迷茫迷茫2778 days ago533

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:26:57

    Why so many spelling mistakes?

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:26:57

    ReactiveCocoas or RxSwift

    reply
    0
  • Cancelreply