如何使用 NSOperation 和调度队列 [英] How to use NSOperation and dispatch queue

查看:54
本文介绍了如何使用 NSOperation 和调度队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向服务器发起请求时使用 NSoperation.Pullparser 将被调用,它将启动一个 o/p &i/p 流.在 connecitonDidRecievedata 中,我正在将来自服务器的数据写入 oStream.Imeediately 我必须调用自定义方法而不是让解析器开始解析.如何处理这种情况.如何处理对解析器的 ostream 调用并处理我的自定义首先是方法,然后在我的自定义方法中,我必须调用解析器.

I am using NSoperation in Initiating request to server.Pullparser will be called and it will initiate a o/p & i/p stream.In connecitonDidRecievedata I am writing data coming from server to oStream.Imeediately I have to call custom method instead of letting the parser start parsing.How to handle this case.How to handle the ostream call to parser and handle my custom method first then in my custom method I have to call the parser.

- (void)run
{     
    [gObjAppDelegatePtr displayActivityIndicator];
    self.m_cObjDownloadOprPtr = [[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(requestToServer) object:nil]autorelease];

    if((NSOperationQueue *)nil == m_cObjDownloadOprQueuePtr)
    {
         m_cObjDownloadOprQueuePtr = [[NSOperationQueue alloc]init];
         [m_cObjDownloadOprQueuePtr setMaxConcurrentOperationCount:1];
    }
    [m_cObjDownloadOprQueuePtr addOperation:self.m_cObjDownloadOprPtr];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //[self.m_cWebData setLength: 0];
    [self.m_cWebData appendData:data];    
    [self.m_cObjOriginalWebDataPtr appendData:data];
    [self attemptToWriteToStream];
}

- (void)attemptToWriteToStream 
{
    if([self.m_cWebData length] > 0) 
    {
        [self saveDownloadedData];
        NSUInteger written = [oStream write:(const uint8_t *)[m_cWebData bytes] maxLength:[m_cWebData length]];
        // NSLog(@"Rcvd Data=%d written = %d",[m_cWebData length],written);
        [m_cWebData replaceBytesInRange:NSMakeRange(0,written) withBytes:"" length:0];
        // NSLog(@"Rcvd Data after Reset =%d ",[m_cWebData length]);[self parseResponse];
    }
}

 self.m_cObjSAXHandler = [self createParser:(id)self];
 self.m_cCurrentDownloadInfo = pObjEntry;self.m_cObjConfig = (MobileCRMConfiguration*)super.m_cObjParent.m_cObjConfiguration;
 m_cIsSuccess = NO;

 self.m_cIsOrganizationMatches = NO;
 [self.m_cObjXmlParser ParseWithStream:iStream];

-(void)ParseWithStream:(NSInputStream *)pInputStream
{
    self.m_cObjXMLParser = [[NSXMLParser alloc] initWithStream:pInputStream];
    self.m_cObjXMLParser setDelegate:m_cObjSAXHandler];
    dispatch_block_t dispatch_block  = ^(void){
        [self.m_cObjXMLParser parse];
    };

    dispatch_queue_t dispatch_queue = dispatch_queue_create("parser.queue", NULL);
    dispatch_async(dispatch_queue, dispatch_block);
    dispatch_release(dispatch_queue);
}

推荐答案

我已经完成了这个 pullparser 的事情.我必须处理解析器委托,流数据在解析完成后应该更新并将剩余的数据写入流.

I have done this pullparser thing.I have to handle the parser delegates and stream data should be updated when it finished parsing and write the leftover data to stream.

这篇关于如何使用 NSOperation 和调度队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆