gpt4 book ai didi

ios - 如何延迟某些行的执行

转载 作者:行者123 更新时间:2023-11-29 01:52:51 24 4
gpt4 key购买 nike

我有按钮单击事件。在其中我正在调用另一个 NSObject 类中的另一个方法。该方法在调度 block 中调用 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 方法后执行后面的部分。解决这个问题的办法是什么?如果我应该使用 block ,我该如何为此编写 block ?请帮助我。

谢谢

已更新

-(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 调用的最佳原生类是 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

关于ios - 如何延迟某些行的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com