Obj-C 中NSOperationQueue 相关

来源:互联网 发布:淘宝3颗心要多少个好评 编辑:程序博客网 时间:2024/06/06 14:21

       之前大概知道这个东西,但一直也没用实际用过,最近想做个社交应用,就要开始搞这个,把网络请求放到队列里面排队,以解决网络请求同时进行,带来的效率以及不稳定等因素。

       NSOperationQueue是个队列,队列里面可以放 NSOperation,NSOperation是个虐基类,不能直接放过NSOperation的对象,常见的办法是继承NSOperation,把操作放进队列中(见ASI中,队列以及继续相关的实现https://github.com/pokeb/asi-http-request)

       在我的代码中,是使用NSInvocationOperation,把要执行的selector放进去,自己做下逻辑即可。


放一段示例代码:

 

NSInvocationOperation*ioRequest = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(requestWork:) object:_dicWork];

 

    

 

    if ([[_oqRequest operations] count] > 0 ) {

 

       NSInvocationOperation* ioLast =[[_oqRequest operations] lastObject];

 

       if(ioLast!= nil)

 

           [ioRequestaddDependency:ioLast];

 

    }

 

    

 

    [_oqRequest addOperation:ioRequest];


0 0
原创粉丝点击