gpt4 book ai didi

iphone - 如何重复 ASIHTTPRequest?

转载 作者:行者123 更新时间:2023-12-03 20:22:15 27 4
gpt4 key购买 nike

给出下面的示例代码:

// ExampleModel.h

@interface ExampleModel : NSObject <ASIHTTPRequestDelegate> {

}

@property (nonatomic, retain) ASIFormDataRequest *request;
@property (nonatomic, copy) NSString *iVar;

- (void)sendRequest;


// ExampleModel.m

@implementation ExampleModel

@synthesize request;
@synthesize iVar;

# pragma mark NSObject

- (void)dealloc {
[request clearDelegatesAndCancel];
[request release];
[iVar release];
[super dealloc];
}

- (id)init {
if ((self = [super init])) {
// These parts of the request are always the same.
NSURL *url = [[NSURL alloc] initWithString:@"https://example.com/"];
request = [[ASIFormDataRequest alloc] initWithURL:url];
[url release];
request.delegate = self;
[request setPostValue:@"value1" forKey:@"key1"];
[request setPostValue:@"value2" forKey:@"key2"];
}
return self;
}

# pragma mark ExampleModel

- (void)sendRequest {
// Reset iVar for each repeat request because it might've changed.
[request setPostValue:iVar forKey:@"iVarKey"];
[request startAsynchronous];
}

@end

# pragma mark ASIHTTPRequestDelegate

- (void)requestFinished:(ASIHTTPRequest *)request {
// Handle response.
}

- (void)requestFailed:(ASIHTTPRequest *)request {
// Handle error.
}

当我从 UIViewController 执行类似 [exampleModel sendRequest] 的操作时,它起作用了!但是,然后我从另一个 UIViewController 再次执行 [exampleModel sendRequest] 并得到:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSOperationQueue addOperation:]:
operation is finished and cannot be enqueued`

我该如何解决这个问题?

最佳答案

您不应尝试重用请求对象。它维持状态。真正设计为在请求结束后处理掉。

该设计不像 NSURLConnection、NSURLRequest、NSURLResponse 类那么干净(基本上将所有三个类混为一谈,并将低级核心基础类包装在下面)。如果您需要处理低级 HTTP 内容,它仍然比以普通方式使用 NSURLConnection 好得多。如果不这样做,高级类有一些优点(例如访问 UIWebView 使用的相同缓存)。

关于iphone - 如何重复 ASIHTTPRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com