【问题标题】:IOS 5.0 - NSURLConnection works but returns Null before completingIOS 5.0 - NSURLConnection 工作,但在完成之前返回 Null
【发布时间】:2012-02-12 16:57:32
【问题描述】:

我有一个名为 functions 的类文件,我在其中保留重复性任务。其中的一个函数称为 GetPrice,它连接到 XML Web 服务,解析 XML 并返回 CarPrice 对象。在返回 CarPrice 对象之前,一切正常。即使在我的 connectionDidFinishLoading 中,它也是 NULL,对象不为 null。

这是我的 GetPrice 函数:

-(CarPrice*)GetPrice:(NSString *)m
{
    NSString *url =[@"http://myUrl.com"];

    dataWebService = [NSMutableData data];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];
    return mp;  //mp is declared as a CarPrice in the @interface section of my funcs class

    //when it gets returned here it is NULL even though....(see below)
}


//Connection Functions=======================


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [dataWebService setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [dataWebService appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
    ParserMetal *pmr = [ParserMetal alloc];
    mp = [pmr parseMetal:responseString];
    //at this point, the mp is fully populated
    //an NSLOG(@"%@", mp.displayPrice); here will show that the mp object is populated
    //however as stated above, when it returns, it is null.
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Error during Connection: %@", [error description]);
}

//End Connection Functions ==================

return mp; 是否在 mp 被填充之前发生?我是否需要在此处使用同步连接来确保在返回之前填充数据?

【问题讨论】:

    标签: ios nsurlconnection nsurlrequest


    【解决方案1】:

    如果我正确理解您的代码,您将调用GetPrice:m:,然后从那里开始连接。使用[connection start]启动连接后,立即返回mp

    这意味着连接已启动,但在它接收到所有数据之前,您已经返回mp。您应该等待接收到数据,然后返回mp

    您可以为此使用同步方法,或者您可以在主类中实现一个方法,该方法将从您的“其他类文件”中定义的connectionDidFinishLoading:connection: 方法中调用。像这样:

    • 开始连接
    • 接收数据...
    • 致电[mainClass didReceiveAllData:mp]

    希望这会有所帮助。

    【讨论】:

    • 通过mainClass,你的意思是调用GetPrice函数的MainViewController吗?我不太确定该怎么做......我遇到的问题是我对函数进行了几次调用,如下所示: CarPrice *hp = [fn GetPrice:@"honda"]; CarPrice *tp = [fn GetPrice:@"toyota"]; CarPrice *cp = [fn GetPrice:@"chevrolet"];我需要分配这些对象,然后才能真正继续应用程序...我不确定如何在 didReceiveAllData:mp] 函数中分配这些对象,并能够将它们保留为不同的对象。
    • 另外,是否有任何人都知道的类似这样的实际工作演示?教程什么的。我是一名 C# 开发人员,所以在这种情况下伪代码对我来说并不适用,因为我没有可以与之比较的示例
    • 关于您的第一条评论:是的。您应该定义一个函数(例如“didReceiveAllData”,但您可以随意调用它),然后在那里处理所有传入的信息。该方法应该在您要调用 [fn GetPrice] 的类中,例如您所说的 MainViewController。我认为这没有一个很好的代码示例。但是,如果您不擅长 Objective C 和委托之类的东西,您可能只想坚持使用同步连接,这在您的情况下更容易使用。
    • 我能够将我的函数更改为 void 函数,然后在 connectionDidFinishLoading 中调用我的主视图控制器中的一个函数并通过它传递数据填充对象,而不是返回一个 Null 对象。我现在遇到的问题是接受对象的函数似乎无法在我的主视图上更新 label.text .....
    • 我认为那是因为您现在正在使用多个线程。您有一个线程正在从 Web 加载数据,并且您有一个运行 UI 的线程。您只能从处理您的 UI 的线程更改您的 UI - 您现在尝试从 NSURLConnection 生成的线程中执行此操作。尝试查找 NSNotificationCenter,你可能会出来。否则,您可能想开始一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多