0

我正在使用 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{


  }
4

1 回答 1

0

您可以根据 URL 中的唯一性来执行此操作......或者您可以添加一个参数作为

.h文件中

@property(nonatomic) int callingClassValue; //0 for VC1, 1 for VC2, and so on 

.m文件中

@synthesize callingClassValue;

在您创建该ServiceHandle对象的位置,将您的值分配给callingClassValue.

然后根据它的值,你可以调用相关的委托方法。

于 2015-01-27T07:17:05.167 回答