【问题标题】:NSURLCONNECTION didReceiveData: storing NSDATA in NSMutableData errorNSURLCONNECTION didReceiveData:在 NSMutableData 中存储 NSDATA 错误
【发布时间】:2014-07-21 12:12:06
【问题描述】:

我正在使用返回大量数据的 NsUrlConnection 调用 Web 服务。这是多次调用 didReceiveData 方法,当我打印出来时,NSLog 显示数据是正确的。

然而,我现在遇到的问题与将 didReceiveData 方法中返回的 NSData 存储到 NSMutableData ivar 以供以后使用有关。

我在http://cagt.bu.edu/w/images/8/8b/URL_Connection_example.txt 找到了它的使用示例,经过一些修改,我得到了以下内容:

.h

@property (nonatomic,assign) NSMutableData *receivedData;

.m

@synthesize receivedData;


-(Boolean) getCategories {

MCUtility * util = [MCUtility alloc];
NSString * strUrl = [NSString stringWithFormat:@"%@", [util getCategoryUrl]];
NSLog(@"%@", [NSString stringWithFormat:@"%@", strUrl]);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
    return FALSE;
} else {    
   self.receivedData = [NSMutableData data];
    [receivedData setLength:0]; //<<OK call
}

return TRUE;

}

#pragma NSURLCONNECTION

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse object.

// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.

// receivedData is an instance variable declared elsewhere.
if (receivedData != nil)
    [receivedData setLength:0]; //<< thread EXC_BAD_ACCESS (code 2, address....)
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Data Received");
NSLog(@"%@", [NSString stringWithFormat:@"%@", [NSString stringWithUTF8String:[data bytes]]]);
[receivedData appendData:data];  //<< (2) thread EXC_BAD_ACCESS (code 2, address....)

在设置方法 getCategories 中,我使用创建初始 NSMutableData 变量

self.receivedData = [NSMutableData data];

((1)在此之后,我添加了一个对 [receivedData setLength:0]; 的测试调用;这实际上不是代码的一部分,但在这里可以正常工作)

对 Web 服务的调用很好,所以我开始在 didReceiveData 中接收数据。

在方法 didReceiveResponse 中,示例说明了对 [receivedData setLength:0] 的调用;是必须的。此时我得到一个

thread EXC_BAD_ACCESS (code 2, address....)

..在我之前在 (1) 中所述创建 iVar 对象之后,我没有收到此错误

在方法 didReceiveData 中,当我尝试在 (2) 处将数据分配给 NSMutableData 时,我也会收到此错误。

所以作为一个客观的 c 新手,我认为这与我用来存储数据的 iVar 有关。有没有什么简单而明显的遗漏?

【问题讨论】:

  • 将您的资源从 assign 更改为 strong

标签: objective-c nsurlconnection


【解决方案1】:

receivedData 正在被释放。

[NSMutableData data] 返回一个自动释放的变量。您需要保留它或更改您的属性定义以保留它。

@property (nonatomic,retain) NSMutableData *receivedData; 应该可以工作。

如果使用 ARC,则将属性设为

@property (nonatomic,strong) NSMutableData *receivedData;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多