JAK
JAK

Reputation: 6471

WebService response issue in my iOS app

I am using web service handle class for implementing all web services used in my app(this singleton Class name ‘ServiceHandle’).From my viewController call service and its response get back from my viewController via delegate.This is the process i am used.When i calling continuously the webservice from different viewcontroller,then clashing the responses.(can’t get service response for which service i am calling.sometime service response changed).How to solve this issue?.Please go through my code.

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

. ServiceHandle.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];
}
}

ViewController.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{


  }

Upvotes: 0

Views: 235

Answers (1)

Ahmed Z.
Ahmed Z.

Reputation: 2337

You can either do that based on uniqueness in your URLs... or you can add a parameter as

in .h file

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

in .m file

@synthesize callingClassValue;

and where u create that ServiceHandle's object, assign your value for callingClassValue.

Then based on its value, u can call the relative delegate methods.

Upvotes: 0

Related Questions