gpt4 book ai didi

ios - 警报 View 需要很长时间才能弹出

转载 作者:行者123 更新时间:2023-12-01 17:40:57 28 4
gpt4 key购买 nike

将数据发布到 json Web 服务后,我需要提醒用户数据是否保存成功。我对此没有任何问题,但是在日志中得到响应“数据保存成功”后, View 需要很长时间(几乎 40-50 秒)才能显示警报 View 。一旦我在几秒钟内得到响应,谁能帮我获得警报 View ?这就是我所做的

NSURL *url = [NSURL URLWithString:@"some url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON Data not posted!");
[activity stopAnimating];
UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertMessage show];
}
else
{
[activity startAnimating];
NSLog(@"JSON data posted! :)");
NSError *error = Nil;
NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

NSLog(@"Response is %@", jsonObject);

NSString *code = [jsonObject valueForKey:@"Code"];
NSLog(@"Code value = %@", code);
if([code intValue] == 0)
{
[activity stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data updated!" message:@"Entered data above has been saved in the database successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
[activity stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data not saved" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
}

}

}];
}

最佳答案

我认为您正在从单独的线程调用警报 View 代码。所有 UI 元素都需要从主线程处理。

在您的情况下,您应该更改为以下内容,

[activity performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

或者你可以用 GCD 做到这一点,
dispatch_async(dispatch_get_main_queue(), ^{
[activity stopAnimating];
UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertMessage show];
});

希望有帮助!

关于ios - 警报 View 需要很长时间才能弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971535/

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