Приведенный ниже пример кода:
// 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.
}
Когда я делаю что-то вроде [exampleModel sendRequest]
из UIViewController
, это работает! Но затем я делаю [exampleModel sendRequest]
снова из другого UIViewController
и получаю:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSOperationQueue addOperation:]:
operation is finished and cannot be enqueued`
Как я могу это исправить?