这可能是一个简单的问题,但我现在已经盯着这一段时间了,我找不到它了!
我有一个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的委托时,它会崩溃,[请求setDelegate:self]。我认为它与第一个函数中的Autoreleasing有关。但我不知道如何解决它!
编辑:
这就是我初始化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:)];
答案 0 :(得分:5)
和你一样有同样的问题。问题是,它在调用委托方法之前被释放。只需在start方法中加retain
,在完成的方法中加release
(不要忘记在错误方法中释放它)。只要请求正在执行,这将使对象处于生命中。
答案 1 :(得分:1)
请使用以下命令将代码发布到创建SOAPRequest对象实例的位置:
+ (SoapRequest*) create: (NSObject*) target endPoint: (NSString*) endPoint action: (SEL) action soapAction: (NSString*) soapAction postData: (NSString*) postData;
您可能需要保留它。顺便说一句,你应该使用'id'而不是NSObject *作为你的目标参数,因为你的目标将不是NSObject(它将是一个子类)的实例。
答案 2 :(得分:1)
如果仅在将self(SOAPRequest)设置为委托时发生崩溃,您确定自身未被释放(那么当ASIHTTPRequest调用其委托中已完成的已完成时,就会发生错误访问)
- (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;
}
当你这样做时,请求是自动释放的,你应该确保它被某人保留,直到它发送的ASIHTTPRequest完成或失败。
答案 3 :(得分:0)
保留确实是一个不错的选择,我在我的代码中发现了一些拼写错误,并且已经弄明白我的错误。
我必须处理中间的课程:
- (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;
}