IOS 5.0-NSURLConnection工作,但在完成之前返回Null

IOS 5.0-NSURLConnection工作,但在完成之前返回Null,ios,nsurlconnection,nsurlrequest,Ios,Nsurlconnection,Nsurlrequest,我有一个名为functions的类文件,我在其中保存重复的任务。其中一个函数叫做GetPrice,它连接到一个XMLWeb服务,解析XML并返回一个CarPrice对象。在返回CarPrice对象之前,一切都很正常。即使在我的ConnectionIDFinishLoading中,对象不是NULL,它也是NULL 以下是我的GetPrice函数: -(CarPrice*)GetPrice:(NSString *)m { NSString *url =[@"http://myUrl.com"

我有一个名为functions的类文件,我在其中保存重复的任务。其中一个函数叫做GetPrice,它连接到一个XMLWeb服务,解析XML并返回一个CarPrice对象。在返回CarPrice对象之前,一切都很正常。即使在我的ConnectionIDFinishLoading中,对象不是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 ==================

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

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

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

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

  • 启动连接
  • 接收数据
  • 调用
    [mainClass didReceiveAllData:mp]

希望这能有所帮助。

在mainClass中,您是指调用GetPrice函数的MainViewController吗?我真的不知道怎么做。。。。我遇到的问题是,我对函数进行了多次调用,如下所示:CarPrice*hp=[fn GetPrice:@“honda”];CarPrice*tp=[fn GetPrice:@“丰田”];CarPrice*cp=[fn GetPrice:@“雪佛兰”];我需要先分配这些对象,然后才能真正继续应用程序…我不确定如何在
didReceiveAllData:mp]
函数中分配这些对象,并将它们作为不同的对象保留。此外,是否有任何人都知道的此类实际工作演示?辅导之类的。我是一名C#开发人员,所以在这种情况下伪代码对我来说并不适用,因为我没有任何例子可以将它与您的第一条评论进行比较:是的。您应该定义一个函数(例如'didReceiveAllData',但是您可以调用任何您想要调用的函数),然后在那里处理所有传入的信息。该方法应该在您要调用[fn GetPrice]的类中,例如您所说的MainViewController。我认为这方面没有一个好的代码示例。但是,如果你不擅长目标C和委托之类的东西,你可能只想坚持同步连接,这在你的情况下更容易使用。我可以将我的函数更改为void函数,然后不再返回Null对象,在ConnectionIDFinishLoading中,我调用了主视图控制器中的一个函数,并将数据填充的对象传递回主视图控制器。我现在遇到的问题是,接受对象的函数似乎无法更新主视图上的label.text…..我认为这是因为您现在使用多个线程。您有一个从web加载数据的线程,还有一个运行UI的线程。您只能从处理UI的线程更改UI-您现在正试图从NSURLConnection生成的线程中更改UI。试着查一下NSNotificationCenter,也许你会发现。否则,您可能需要开始一个新问题。