ragul ml ragul ml - 6 months ago 23
iOS Question

How to parse json data using singleton class

I have a singleton class where i have implemented a method to parse json data through URL. The code is as below

-(id)parseJsonDataWIthURL:(NSString *)url :(NSString*)datumm
{

NSMutableDictionary *arrrrr=[[NSMutableDictionary alloc]init];



NSMutableURLRequest *reqqq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];

NSData *dataaa=[datumm dataUsingEncoding:NSUTF8StringEncoding];
[reqqq setHTTPMethod:@"POST"];
[reqqq setHTTPBody:dataaa];

NSURLSessionConfiguration *configg=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession*sessionn=[NSURLSession sessionWithConfiguration:configg delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

NSURLSessionDataTask *taskk=[sessionn dataTaskWithRequest:reqqq completionHandler:^(NSData *data,NSURLResponse *responce,NSError *error){
if(error)
{
NSLog(@"%@", [error localizedDescription]);
}else{
NSMutableDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];
NSLog(@"data %@",d);
[arrrrr setDictionary:d];
}
}];
[taskk resume];


return arrrrr;
}


The method returns no values, it is because the blocks takes time to execute within that method returns the result. So is there any way to wait until block completes and return the value.

Answer

ragul ml,

Simplest solution I can suggest is to use blocks :)

Here is how you can achieve it :) modify -(id)parseJsonDataWIthURL:(NSString *)url :(NSString*)datumm to,

-(void)parseJsonDataWIthURL:(NSString *)url :(NSString*)datumm withCompletionBlock:(void(^)(NSMutableArray *))completionBlock 
{

    NSMutableDictionary *arrrrr=[[NSMutableDictionary alloc]init];



            NSMutableURLRequest *reqqq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];

            NSData *dataaa=[datumm dataUsingEncoding:NSUTF8StringEncoding];
            [reqqq setHTTPMethod:@"POST"];
            [reqqq setHTTPBody:dataaa];

            NSURLSessionConfiguration *configg=[NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession*sessionn=[NSURLSession sessionWithConfiguration:configg delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

            NSURLSessionDataTask *taskk=[sessionn dataTaskWithRequest:reqqq completionHandler:^(NSData *data,NSURLResponse *responce,NSError *error){
                if(error)
                {
                    NSLog(@"%@", [error localizedDescription]);
                    completionBlock(nil)
                }else{
                    NSMutableDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];
                    NSLog(@"data %@",d);
                    [arrrrr setDictionary:d];
                    if (completionBlock) {
                          completionBlock(arrrrr);
                    }
                }
            }];
            [taskk resume];
}

finally modify your call as,

[[YourSingletonClass sharedInstance] parseJsonDataWIthURL:your_url :datumm withCompletionBlock:^(NSMutableArray *array) {
        if (array){
            //web service is successful
        }
    }];