设置委托时 ASIHTTPRequest 崩溃?
这可能是一个简单的问题,但我已经盯着这个问题有一段时间了,但我找不到它!
我有一个如下所示的 SOAPRequest 类:
@interface SoapRequest : NSObject
@property (retain, nonatomic) NSURL *endPoint;
@property (retain, nonatomic) NSString *soapAction;
@property (retain, nonatomic) NSString *userName;
@property (retain, nonatomic) NSString *passWord;
@property (retain, nonatomic) NSString *postData;
@property (retain, nonatomic) NSObject *handler;
@property SEL action;
+ (SoapRequest*) create: (NSObject*) target endPoint: (NSString*) endPoint action: (SEL) action soapAction: (NSString*) soapAction postData: (NSString*) postData;
- (id)sendSynchronous;
- (void) send;
@end
实现如下:
@synthesize endPoint = _endPoint, soapAction = _soapAction, userName = _userName, passWord = _passWord, postData = _postData, action = _action, handler = _handler;
+ (SoapRequest*) create: (NSObject*) target endPoint: (NSString*) endPoint action: (SEL) action soapAction: (NSString*) soapAction postData: (NSString*) postData
{
SoapRequest *request = [[SoapRequest alloc] init];
request.endPoint = [NSURL URLWithString:endPoint];
request.soapAction = soapAction;
request.handler = target;
request.action = action;
request.postData = postData;
return [request autorelease];
}
我的发送函数:
- (void) send
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.endPoint];
[request setDelegate:self];
[request addRequestHeader:@"Content-Length" value: [NSString stringWithFormat:@"%d",[self.postData length]]];
[request addRequestHeader:@"Content-Type" value:@"text/xml"];
if(self.soapAction)
{
[request addRequestHeader:@"soapAction" value:self.soapAction];
}
[request appendPostData:[self.postData dataUsingEncoding:NSUTF8StringEncoding]];
[request startAsynchronous];
}
我确实有默认的 ASIHTTPRequest 方法来侦听错误或完成,我的完成如下所示:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self.handler performSelector:self.action];
}
情况是,当我想要创建此类时,它会崩溃ASIHTTPRequest 的委托,[request setDelegate:self]。我认为这与第一个函数中的自动释放有关。但我不知道如何解决它!
编辑:
这就是我初始化 SoapRequest 的方式:
- (SoapRequest*) DefinedEntities: (id) _target action: (SEL) _action
{
// some data inits
SoapRequest *request = [SoapRequest create: _target endPoint:endPoint action:_action soapAction:soapAction postData:xmlString];
[request send];
return request;
}
我的初始化如下:
Metadata *service = [[Metadata alloc] init];
[service DefinedEntities:self action:@selector(MetadataFinished:)];
It will be probably a simple problem, but I have been staring on this for a while now and I can't find it!
I have a SOAPRequest class like following:
@interface SoapRequest : NSObject
@property (retain, nonatomic) NSURL *endPoint;
@property (retain, nonatomic) NSString *soapAction;
@property (retain, nonatomic) NSString *userName;
@property (retain, nonatomic) NSString *passWord;
@property (retain, nonatomic) NSString *postData;
@property (retain, nonatomic) NSObject *handler;
@property SEL action;
+ (SoapRequest*) create: (NSObject*) target endPoint: (NSString*) endPoint action: (SEL) action soapAction: (NSString*) soapAction postData: (NSString*) postData;
- (id)sendSynchronous;
- (void) send;
@end
Implementation like following:
@synthesize endPoint = _endPoint, soapAction = _soapAction, userName = _userName, passWord = _passWord, postData = _postData, action = _action, handler = _handler;
+ (SoapRequest*) create: (NSObject*) target endPoint: (NSString*) endPoint action: (SEL) action soapAction: (NSString*) soapAction postData: (NSString*) postData
{
SoapRequest *request = [[SoapRequest alloc] init];
request.endPoint = [NSURL URLWithString:endPoint];
request.soapAction = soapAction;
request.handler = target;
request.action = action;
request.postData = postData;
return [request autorelease];
}
And my send function:
- (void) send
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.endPoint];
[request setDelegate:self];
[request addRequestHeader:@"Content-Length" value: [NSString stringWithFormat:@"%d",[self.postData length]]];
[request addRequestHeader:@"Content-Type" value:@"text/xml"];
if(self.soapAction)
{
[request addRequestHeader:@"soapAction" value:self.soapAction];
}
[request appendPostData:[self.postData dataUsingEncoding:NSUTF8StringEncoding]];
[request startAsynchronous];
}
I do have the default ASIHTTPRequest methods to listen for error or finish, my finish looks as following:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self.handler performSelector:self.action];
}
The case is, it crashes when I want to make this class a delegate for the ASIHTTPRequest, [request setDelegate:self]. I figured it has something to do with the Autoreleasing in the first function. But I don't have any idea how to fix it!
Edit:
This is how I init my SoapRequest:
- (SoapRequest*) DefinedEntities: (id) _target action: (SEL) _action
{
// some data inits
SoapRequest *request = [SoapRequest create: _target endPoint:endPoint action:_action soapAction:soapAction postData:xmlString];
[request send];
return request;
}
And this i init as following:
Metadata *service = [[Metadata alloc] init];
[service DefinedEntities:self action:@selector(MetadataFinished:)];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
和你有同样的问题。问题是,它在调用委托方法之前被释放。只需在开始方法中放置一个
retain
,在完成方法中放置一个release
(并且不要忘记在错误方法中释放它)。只要请求正在执行,这就会让对象保持生命。Had the same problem like you. The problem is, it gets deallocated before the delegate method gets called. Simply put a
retain
in your start method and arelease
in your finished method (and don't forget to release it in the error methods). This will let the object at life as long as the request is performing.请发布您使用以下方法创建 SOAPRequest 对象实例的代码:
您可能需要保留它。顺便说一句,您应该使用“id”而不是 NSObject* 作为目标参数,因为您的目标不会是 NSObject 的实例(它将是一个子类)。
Please post the code where you're creating the instance of a SOAPRequest object using:
You may need to retain it. As an aside, you should use 'id' instead of NSObject* for your target parameter, as your target will not be an instance of NSObject (it will be a subclass) of sorts.
如果仅当您将 self (SOAPRequest) 设置为委托时才发生崩溃,那么您确定 self 没有被释放(那么当 ASIHTTPRequest 对其不再存在的委托调用 did finish 时,就会发生错误的访问)。
当您执行此操作时,请求会自动释放,并且您应该确保它由某人保留,直到它发送的 ASIHTTPRequest 完成或失败。
If the crash only occurs when you set self (SOAPRequest) as the delegate, are you sure that self isn't being deallocated (then the bad access occurs when the ASIHTTPRequest calls the did finish on its delegate which no longer exists).
When you do this, request is autoreleased and you should make sure it is retained by someone until the ASIHTTPRequest it sent has finished or failed.
保留确实是一个不错的选择,我在代码中发现了一些拼写错误,并找出了我做错了什么。
我必须处理中间的课程:
Retaining was indeed a good option, I found some typo's in my code and figured out what i did wrong already.
I had to do with the class in-between: