如何延迟某些行的执行

时间:2015-07-04 04:54:32

标签: ios objective-c-blocks

我有按钮点击事件。在里面我调用另一个NSObject类中的另一个方法。该方法在调度块中调用Web服务。我点击按钮就像这样

-(IBAction)Loging:(id)sender
{

NSString *strParam=[NSString stringWithFormat:@"userName=%@&password=%@&ipAddress=%@",@"admin",@"1234",@"127.0.0.1"];

NSString *str=[wb prepareURL:@"account/users?" :strParam :NO :YES :nil];
 NSLog(@"data array %@",dm.arrayData);
 NSLog(@"str========= %@",str);
}

我的问题是这样的。此arrayDatastr打印为空,因为这两行在prepareURL完成执行之前执行。我想在完全完成prepareURL方法后执行后面的部分。这会是什么解决方案?如果我应该使用块,我怎么能写一个块呢?请帮帮我。

由于

已更新

-(NSString *)prepareURL :(NSString *)appendString :(NSString *)bindedParams :(BOOL)isauthorized :(BOOL)isGet :(NSMutableDictionary *)jsonBody

 {

dm=[Datamanager sharedManager];
NSString *strToken=dm.strAuthToken;
NSString *strServerURL=[[[NSBundle mainBundle]infoDictionary] objectForKey:@"BaseURL"];
NSString *strfullURL=[NSString stringWithFormat:@"%@%@%@",strServerURL,appendString,bindedParams];
NSURL *url=[NSURL URLWithString:strfullURL];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

if (isauthorized) {

    [request addValue:[NSString stringWithFormat:@"Bearer %@", strToken] forHTTPHeaderField:@"Authorization"];
}

NSString *str=[self GetPostPreperation:request:isGet:jsonBody];

return str;



 }

这是我的GetPostPreperation方法

-(NSString *)GetPostPreperation :(NSMutableURLRequest *)request :(BOOL)isGet :(NSMutableDictionary *)jsonBody


 {


 NSError *error=nil;
 __block NSString *strReturn;

if (isGet) {
     [request setHTTPMethod:@"GET"];
}

else
{
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
     NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonBody
                                                       options:NSJSONWritingPrettyPrinted error:&error];
    NSString *string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"Json string %@",string);
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData];


}


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{
    NSError *error = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error: &error];
    NSString *string = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


    NSLog(@"Response string %@",string);

    if (!error) {

        NSLog(@"No Error");

        NSError *jsonerror=nil;
        NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: returnData options: NSJSONReadingAllowFragments error: &jsonerror];

        if (!jsonArray) {
            NSLog(@"Error parsing JSON: %@", jsonerror);
            strReturn=[[NSString alloc] initWithFormat:@"%@",jsonerror];
        } else {

            dm=[Datamanager sharedManager];
            [dm.arrayData addObjectsFromArray:jsonArray];


            }

    }
    else
    {
        NSLog(@"Error %@",error);
        strReturn=[NSString stringWithFormat:@"%@",error];

    }

});


return strReturn;




  }

1 个答案:

答案 0 :(得分:1)

iOS中api调用的最佳本机类目前为NSURLSession

使用此课程:

-(NSString *)prepareURL :(NSString *)appendString :(NSString *)bindedParams :(BOOL)isauthorized :(BOOL)isGet :(NSMutableDictionary *)jsonBody completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error) ) completion{
   NSURLSession *session = [NSURLSession sharedSession];
   NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //request is your NSURLRequest object
    //use the data here to get your wanted result
    completion(data,response,error); //send data to calling class
    }];
    [task resume];
}

关于如何使用块,您可以参考this website