Asynchronous iOS 5:pList的异步检索-接收的数据始终为0字节
嗨,我正在尝试使用NSURLConnection委托方法将存储在服务器上的属性列表获取到我的设备上 我在Jeff LaMarche的iOS 3书中看到了一些教程,基本上我正在尝试实现这些方法 这些是实现方法 但我总是收到:“收到0字节的数据”我不明白:Asynchronous iOS 5:pList的异步检索-接收的数据始终为0字节,asynchronous,ios5,nsurlconnection,Asynchronous,Ios5,Nsurlconnection,嗨,我正在尝试使用NSURLConnection委托方法将存储在服务器上的属性列表获取到我的设备上 我在Jeff LaMarche的iOS 3书中看到了一些教程,基本上我正在尝试实现这些方法 这些是实现方法 但我总是收到:“收到0字节的数据”我不明白: #pragma -NSURLCOnnection Delegate Methods - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
#pragma -NSURLCOnnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
[receivedData setLength:0];
}
//==============================================================================
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
if(!self.receivedData)
{
NSLog(@"Received Data was nil");
}
}
//==============================================================================
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert1 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:[NSString stringWithFormat:@" Connection Failed! Error: %@ ,(URL:%@)", [error localizedDescription] ,[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert1 show];
NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
//Now take the data and convert it into a propertylist
NSData *plistData = self.receivedData;
NSPropertyListFormat format;
NSString *error;
id pList = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
if(!pList)
{
NSLog(@"There was an error converting data received into a propertyList");
NSLog(error);
}
self.receivedData = nil;
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
-(void)retrieveFileFromServerAsynchronously
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
if(![self getURLToGetFileFrom])
{
UIAlertView *alert1 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"InvalidPath"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert1 show];
}
if(![self getFileName])
{
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Invalid FileName"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert2 show];
}
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
NSMutableString *urlString = [[NSMutableString alloc] initWithString:[self getURLToGetFileFrom]];
[urlString appendString:[self getFileName]];
NSLog((@"Fetching file from URL %@", urlString));
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.00 ];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con)
{
NSMutableData *data = [[NSMutableData alloc] init ];
self.receivedData = data;
if(!self.receivedData)
NSLog(@"Received Data was nil");
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
else
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Couldn't connect to remote server"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
}
}
我在控制台上得到上面的输出。有人能告诉我从哪里开始修理这个吗
fileFetcher.m-[fileFetcher连接:didReceiveResponse:]183fileFetcher.m-[fileFetcher连接:didReceiveData:]190
收到的数据为零
fileFetcher.m-[fileFetcher connectiondFinishLoading:]215
成功!接收到0字节的数据
将收到的数据转换为propertyList时出错
流的字节太少
fileFetcher.m-[fileFetcher connectiondFinishLoading:]236
非常感谢。异步在RetrieveFileFromServers中设置receivedData后,最简单的原因是它是否因为弱引用而被置空
当您alloc/init本地“data”变量时,您创建了一个强引用,但随后将其分配给一个调零弱引用并从方法返回,导致“data”超出范围,丢失唯一的强引用并导致弱引用receivedData被调零。receivedData不是弱的,是吗?是的,它是弱的。天哪,那就是问题所在。哇,真奇怪,我花了大约一天的时间调试这个东西!!iOS 5的这些变化快把我逼疯了!!谢谢。你可以“回答”我的问题,这样我就可以接受你的答案。添加了一个更详细的答案,解释为什么会发生这种情况。