我正在使用 Web 服务句柄类来实现我的应用程序中使用的所有 Web 服务(这个单例类名称“ServiceHandle”)。从我的 viewController 调用服务及其响应通过委托从我的 viewController 返回。这是我使用的过程。当我从不同的视图控制器连续调用 Web 服务时,响应发生冲突。(无法获得我正在调用的服务的服务响应。有时服务响应已更改)。如何解决此问题?请查看我的代码。
ServiceHandle.h
@protocol serviceDelegate <NSObject>
-(void)successFullResponse:(ASIHTTPRequest *)req;
-(void)failedResponse:(ASIHTTPRequest *)req;
@end
@interface ServiceHandle : NSObject
@property(nonatomic,strong)NSOperationQueue *queue;
@property (nonatomic,weak)id<serviceDelegate>delegate;
+ (id)sharedInstance;
-(void)loginWithUserName:(NSString *)userName;
-(void)userReg:(NSDictionary*)userDict;
@end
. 服务句柄.m
static ServiceHandle *sharedInstance = nil;
@implementation ServiceHandle
@synthesize delegate,queue;
+(ServiceHandle *)sharedInstance{
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL]init];
}
return sharedInstance;
}
- (id)init
{
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
}
return self;}
-(void)loginWithUserName:(NSString *)userName{
if (![self queue]) {
[self setQueue:[[NSOperationQueue alloc] init]];
}
NSString *path=[NSString stringWithFormat:@"%@userLogin",webserviceURL];
NSString *lattitude=[[NSUserDefaults standardUserDefaults] objectForKey:@"mylatitude"];
NSString *longitude=[[NSUserDefaults standardUserDefaults] objectForKey:@"mylongitude"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]];
[request setTimeOutSeconds:30];
[request setPostValue:userName forKey:@"userId"];
[request setPostValue:lattitude forKey:@"latitude"];
[request setPostValue:longitude forKey:@"longitude"];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setDidFinishSelector:@selector(requestSuccess:)];
[request setDidFailSelector:@selector(requestFail:)];
request.queuePriority = NSOperationQueuePriorityHigh;
request.delegate = self;
[[self queue]addOperation:request];
}
-(void)requestSuccess:(ASIHTTPRequest *)request{
if ([self.delegate respondsToSelector:@selector(successFullResponse:)]) {
[self.delegate successFullResponse:request];
}
}
-(void)requestFail:(ASIHTTPRequest *)request{
if ([self.delegate respondsToSelector:@selector(failedResponse:)]) {
[self.delegate failedResponse:request];
}
}
视图控制器.m
-(IBAction)clickLogin:(id)sender{
ServiceHandle *sharedObj = [ServiceHandle sharedInstance];
sharedObj.delegate = self;
[sharedObj loginWithUserName:@“usrName”];
}
-(void)successFullResponse:(ASIHTTPRequest *)req{
NSString *responseString = [req responseString];
NSLog(@"responseString is%@",responseString);
NSDictionary *responseDict = [responseString JSONValue];
NSLog(@"dict is :%@",responseDict);
}
-(void)failedResponse:(ASIHTTPRequest *)req{
}