使用dispatch_async进行混淆

时间:2013-08-13 09:00:51

标签: ios

Iam调用json从具有两个不同URL的viewcontroller解析函数。

- (void)viewDidLoad
{

[super viewDidLoad];

 parseObj=[[json_parser alloc]init];

NSString *group_url=@"http://urlforgroup";
NSString *drug_url =@"http:/urlfordrug";

mConcurrentQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(mConcurrentQueue, ^{tFunct1(group_url,@"group_url");});
dispatch_async(mConcurrentQueue, ^{tFunct1(drug_url,@"drug_url");});
}

void (^tFunct1)(NSString *,NSString *)=^(NSString *base_url,NSString *stat_val){


dispatch_async(dispatch_get_main_queue(), ^(void){

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:[NSString stringWithFormat:@"%@",stat_val] forKey:@"check_url"];
    [parseObj parseUrl:base_url];
});


};

@end

//////////////////////// json parser//////////

 -(void)parseUrl:(NSString *)baseUrl
{


NSLog(@"URL%@",baseUrl);
 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *savedValue = [prefs stringForKey:@"check_url"];

NSLog(@"%@",savedValue);// showing different value group_url then drug_url

NSURL *url=[NSURL URLWithString:baseUrl];

NSURLRequest *request=[NSURLRequest requestWithURL:url];

conn=[NSURLConnection connectionWithRequest:request delegate:self];

if(conn)
{

    mdata=[[NSMutableData alloc]init];
}

}


 - (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    // getting an NSString
    NSString *savedValue = [prefs stringForKey:@"check_url"];
    NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:mdata options:0 error:nil];

    NSLog(@"connectionDidFinishLoading %@",savedValue);// showing same value drug url


    if([savedValue isEqualToString:@"grp_url"])
      {


        NSLog(@"some code");

      }
    else
      {
            NSLog(@"some other code"); 


     }



   }

我想根据可验证的“savedValue”的值来编写不同的解析代码。但我的问题是在connectionDidFinishLoading内部我对url.Please帮助有相同的值“drug_url”。

1 个答案:

答案 0 :(得分:0)

您还需要实施

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  [mdata appendData:data];
}

为此起作用