2016-05-25 17 views
0

我有一個單例類,我已經實現了一個通過URL解析json數據的方法。代碼如下如何使用單例類來解析json數據

-(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; 
} 

該方法返回沒有值,這是因爲塊需要時間在該方法內執行返回結果。那麼有什麼方法可以等到塊完成並返回值。

+0

使用完成塊,通知或委託。但由於它是異步的,'grrrrr'在返回時將不包含任何值。 – Larme

+0

如何在我的代碼中使用完成塊。可能嗎? –

回答

0

ragul毫升,

我可以建議

最簡單的解決方法是使用塊:)

這裏是你如何能實現呢:)修改-(id)parseJsonDataWIthURL:(NSString *)url :(NSString*)datumm到,

-(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]; 
} 

最後修改您的通話as,

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

非常感謝你sandeep,它爲我工作。 withCompletionBlock是一個自定義塊嗎? –

+0

是的:)你已經在方法定義中聲明:)這是塊定義(無效(^)(NSMutableArray *))這意味着這個塊作爲參數NSMutableArray不返回任何東西:)'^'表示它是一個阻止:) –