HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 后台线程上的NSURLConnection委托方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_450_2@
EDIT2 – 重写了这个问题

我想在后台进行一些Web服务通信.我使用Sudzc作为httprequests的处理程序,它的工作原理如下:

SudzcWS *service = [[SudzcWS alloc] init];
[service sendorders:self withXML:@"my xml here" action:@SELEctor(handleordersending:)];
[service release];

它将一些XML发送到Web服务,并在指定的选择器中处理响应(在这一个中,一个布尔值):

- (void)handleordersending:(id)value
{ 
//some controls  
    if ([value boolValue] == YES)
    {
        //my stuff
    }
}

当我尝试在我的sendorders:withXML:action:方法上使用Grand Central Dispatch时,我注意到没有调用选择器.我相信原因是NSURLConnection委托消息被发送到创建连接的线程但是线程没有那么久,它在方法完成时结束,杀死任何给委托的消息.

问候

EDIT1
[请求发送]方法

- (void) send {
//dispatch_async(BACkgroundQueue,^(void){
    // If we don't have a handler,create a default one
    if(handler == nil) {
        handler = [[SoapHandler alloc] init];
    }

    // Make sure the network is available
    if([SoapReachability connectedToNetwork] == NO) {
        NSError* error = [NSError errorWithDomain:@"SudzC" code:400 userInfo:[NSDictionary DictionaryWithObject:@"The network is not available" forKey:NSLocalizedDescriptionKey]];
        [self handleError: error];
    }

    // Make sure we can reach the host
    if([SoapReachability hostAvailable:url.host] == NO) {
        NSError* error = [NSError errorWithDomain:@"SudzC" code:410 userInfo:[NSDictionary DictionaryWithObject:@"The host is not available" forKey:NSLocalizedDescriptionKey]];
        [self handleError: error];
    }

    // Output the URL if logging is enabled
    if(logging) {
        NSLog(@"Loading: %@",url.absoluteString);
    }

    // Create the request
    NSMutableURLrequest* request = [NSMutableURLrequest requestWithURL: url];
    if(soapAction != nil) {
        [request addValue: soapAction forhttpHeaderField: @"SOAPAction"];
    }
    if(postData != nil) {
        [request sethttpR_960_11845@ethod: @"POST"];
        [request addValue: @"text/xml; charset=utf-8" forhttpHeaderField: @"Content-Type"];
        [request sethttpBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];

        if(self.logging) {
            NSLog(@"%@",postData);
        }
    }


    //dispatch_async(dispatch_get_main_queue(),^(void){
        // Create the connection
        conn = [[NSURLConnection alloc] initWithrequest: request delegate: self];
        if(conn) {
                                        NSLog(@" POST DATA %@",receivedData);
            receivedData = [[NSMutableData data] retain];
                        NSLog(@" POST DATA %@",receivedData);
        } else {
            // We will want to call the onerror method SELEctor here...
            if(self.handler != nil) {
                NSError* error = [NSError errorWithDomain:@"Soaprequest" code:404 userInfo: [NSDictionary DictionaryWithObjectsAndKeys: @"Could not create connection",NSLocalizedDescriptionKey,nil]];
                [self handleError: error];
            }
        }
    //});


    //finished = NO;

    //    while(!finished) {
    //        
    //        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    //        
    //    }

//});
}

注释掉的部分是我尝试过的各种东西.最后一部分有效,但我不确定这是不是一个方法.在类的NURLConnection委托方法中,会发生以下情况:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError* error;
if(self.logging == YES) {
    NSString* response = [[NSString alloc] initWithData: self.receivedData     encoding: NSUTF8StringEncoding];
    NSLog(@"%@",responsE);
    [response release];
}

CXMLDocument* doc = [[CXMLDocument alloc] initWithData: self.receivedData options: 0 error: &error];
if(doc == nil) {
    [self handleError:error];
    return;
}

id output = nil;
SoapFault* fault = [SoapFault faultWithXMLDocument: doc];

if([fault hasFault]) {
    if(self.action == nil) {
        [self handleFault: fault];
    } else {
        if(self.handler != nil && [self.handler respondsToSELEctor: self.action]) {

                [self.handler performSELEctor: self.action withObject: fault];


        } else {
            NSLog(@"SOAP Fault: %@",fault);
        }
    }
} else {
    CXMLNode* element = [[Soap getNode: [doc rootElement] withName: @"Body"] childATindex:0];
    if(deserializeTo == nil) {
        output = [Soap deserialize:element];
    } else {
        if([deserializeTo respondsToSELEctor: @SELEctor(initWithNode:)]) {
            element = [element childATindex:0];
            output = [deserializeTo initWithNode: element];
        } else {
            NSString* value = [[[element childATindex:0] childATindex:0] stringvalue];
            output = [Soap convert: value toType: deserializeTo];
        }
    }

    if(self.action == nil) { self.action = @SELEctor(onload:); }
    if(self.handler != nil && [self.handler respondsToSELEctor: self.action]) {


            [self.handler performSELEctor: self.action withObject: output];


    } else if(self.defaultHandler != nil && [self.defaultHandler respondsToSELEctor:@SELEctor(onload:)]) {
        [self.defaultHandler onload:output];
    }
}

[self.handler release];
[doc release];
[conn release];
conn = nil;
[self.receivedData release];
}

委托无法发送消息,因为当 – (void)发送完成时它将死亡.

@H_450_2@

解决方法

sendorders方法定义表明它已经设计为以异步方式执行请求.你应该看看sendorders的实现:withXML:action:找出是否是这种情况.

如果没有使用GCD或SudzcWS的代码看到您的实现,很难说出现了什么问题.尽管有上述警告,但以下内容可能会有所帮助.

看起来您可能在完成之前发布了SudzcWS *服务.

下列:

SudzcWS *service = [[SudzcWS alloc] init];
dispatch_async(aQueue,^{
    [sevice sendorders:self withXML:xml action:@SELEctor(handleordersending:)];
}
[service release];

除非SudzcWS保留自己,否则可能会失败.您异步调度块,它将被放入队列,并继续执行方法.服务被释放并在块执行之前或在服务等待来自Web服务器的响应时被释放.

除非另行指定,否则调用选择器将在调用它的同一线程上执行该选择器.做类似的事情:

SudzcWS *service = [[SudzcWS alloc] init];
dispatch_async(aQueue,^{
    [sevice sendorders:self withXML:xml action:@SELEctor(handleordersending:)];
}


- (void)handleordersending:(id)value
{ 
    //some controls  
    //your stuff
    [service release];
}

应该确保sendorders方法和handleordersending:都在队列aQueue上执行,并且该服务在执行选择器之前不会被释放.

这将要求您保留指向服务的指针,以便handleordersending:可以释放它.您可能还想虑简单地挂在单个SudzcWS实例上,而不是每次想要使用它时创建和释放一个,这应该使您的内存管理更容易,并有助于保持您的对象图紧.

@H_450_2@ @H_450_2@
@H_450_2@
@H_450_2@

大佬总结

以上是大佬教程为你收集整理的ios – 后台线程上的NSURLConnection委托方法全部内容,希望文章能够帮你解决ios – 后台线程上的NSURLConnection委托方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。