如何延迟某些行的执行
How to delay the execution of some lines
我有按钮点击事件。在其中,我正在调用另一个 NSObject
class 中的另一个方法。该方法在调度块中调用 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);
}
我的问题是这样的。此 arrayData
和 str
打印为空,因为这两行在 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;
}
目前 iOS 中 api 调用的最佳原生 class 是 NSURLSession
。
使用这个 class:
-(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
我有按钮点击事件。在其中,我正在调用另一个 NSObject
class 中的另一个方法。该方法在调度块中调用 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);
}
我的问题是这样的。此 arrayData
和 str
打印为空,因为这两行在 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;
}
目前 iOS 中 api 调用的最佳原生 class 是 NSURLSession
。
使用这个 class:
-(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