如何使用NSOperation和调度队列

时间:2013-02-01 09:42:52

标签: objective-c nsoperation nsoperationqueue

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

- (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);
}

1 个答案:

答案 0 :(得分:0)

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