Ios 警报视图需要很长时间才能弹出

Ios 警报视图需要很长时间才能弹出,ios,web-services,thread-safety,Ios,Web Services,Thread Safety,将数据发布到json web服务后,我需要提醒用户数据是否已成功保存。我对此没有任何问题,但在日志中获得响应“Data Save successfully”后,视图需要很长时间(近40-50秒)才能显示警报视图。有人能帮我在几秒钟内得到响应后立即获取警报视图吗?这就是我所做的 NSURL *url = [NSURL URLWithString:@"some url"]; NSMutableURLRequest *request = [[NSMutableURLRequest al

将数据发布到json web服务后,我需要提醒用户数据是否已成功保存。我对此没有任何问题,但在日志中获得响应“Data Save successfully”后,视图需要很长时间(近40-50秒)才能显示警报视图。有人能帮我在几秒钟内得到响应后立即获取警报视图吗?这就是我所做的

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];
                }

            }

        }];
    }

我认为您是从一个单独的线程调用警报视图代码。所有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];
});

希望有帮助

我认为您是从一个单独的线程调用警报视图代码。所有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];
});

希望有帮助

您应该在主线程中执行块内的所有UI交互。只需使用下面的代码

dispatch_async(dispatch_get_main_queue(), ^{
   [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];
});

您应该在主线程中执行块内的所有UI交互。只需使用下面的代码

dispatch_async(dispatch_get_main_queue(), ^{
   [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];
});

有趣的是,它能够立即锁定用户交互,但不更新UI。不过,感谢上帝给我们的这封信。我在尝试发布自定义警报时遇到了麻烦,该警报解释了为什么我们需要推送通知,并在使用系统提示之前请求权限,但由于iOS 10的UNUserNotificationSettings功能,您可以异步获取设置,因此从内部触发我的提示会从后台线程触发警报。这很奇怪。有趣的是,它能够立即锁定用户交互,但不更新用户界面。不过,感谢上帝给我们的这封信。我在尝试发布自定义警报时遇到了麻烦,该警报解释了为什么我们需要推送通知,并在使用系统提示之前请求权限,但由于iOS 10的UNUserNotificationSettings功能,您可以异步获取设置,因此从内部触发我的提示会从后台线程触发警报。这很奇怪。