У меня есть файл класса, называемый функциями, где я храню повторяющиеся задачи. Одна из функций называется GetPrice, которая подключается к веб-службе XML, анализирует XML и возвращает объект CarPrice. Все прекрасно работает, пока не пришло время вернуть объект CarPrice. Это NULL, хотя в моем connectionDidFinishLoading объект не равен 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;
до того, как мп заполняется? Нужно ли здесь использовать синхронное соединение, чтобы убедиться, что данные заполнены до возврата?