显示加载消息,直到完全解析json iOS
好的,问题是,在从服务器完全下载数据之前,如何模拟加载消息。我有这个问题,因为当保存下载的json数据的属性仍然为零时,我无法将数据传递给下一个视图控制器。那么,在完全解析Json之前,如何模拟加载消息呢 这是我获取数据的代码显示加载消息,直到完全解析json iOS,ios,json,parsing,loading,Ios,Json,Parsing,Loading,好的,问题是,在从服务器完全下载数据之前,如何模拟加载消息。我有这个问题,因为当保存下载的json数据的属性仍然为零时,我无法将数据传递给下一个视图控制器。那么,在完全解析Json之前,如何模拟加载消息呢 这是我获取数据的代码 -(void)fetchFeed { NSString *requestString = @"some website"; NSURL *url = [NSURL URLWithString:requestString]; NSURLRequest *req = [NSU
-(void)fetchFeed
{
NSString *requestString = @"some website";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.locations = jsonObject[@"someKey"];
NSLog(@"%@", self.locations);
}
];
[dataTask resume];
}
- (void)viewDidLoad
{
[super viewDidLoad];
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self fetchFeed]; //Network activity
dispatch_async(dispatch_get_main_queue(), ^{
//do stuff after json download
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
请核对我的答案。。它和你要找的东西差不多 我已经使用MBProgressHUD显示加载消息 简单到
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//Network activity
dispatch_async(dispatch_get_main_queue(), ^{
//do stuff after json download
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
有关更详细的答案,请查看链接
*************编辑*******************
当您使用NSURLSession
时,它允许您执行后台下载操作。根据您发布的代码,我们不需要使用dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u LOW,0),^{}启动新线程。
请试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchFeed]; //Network activity
}
-(void)fetchFeed
{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
NSString *requestString = @"some website";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.locations = jsonObject[@"someKey"];
NSLog(@"%@", self.locations);
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}] resume];
}
这应该行得通 请查看我的答案。。它和你要找的东西差不多 我已经使用MBProgressHUD显示加载消息 简单到
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//Network activity
dispatch_async(dispatch_get_main_queue(), ^{
//do stuff after json download
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
有关更详细的答案,请查看链接
*************编辑*******************
当您使用NSURLSession
时,它允许您执行后台下载操作。根据您发布的代码,我们不需要使用dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u LOW,0),^{}启动新线程。
请试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchFeed]; //Network activity
}
-(void)fetchFeed
{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
NSString *requestString = @"some website";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.locations = jsonObject[@"someKey"];
NSLog(@"%@", self.locations);
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}] resume];
}
这应该行得通 请查看我的答案。。它和你要找的东西差不多 我已经使用MBProgressHUD显示加载消息 简单到
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//Network activity
dispatch_async(dispatch_get_main_queue(), ^{
//do stuff after json download
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
有关更详细的答案,请查看链接
*************编辑*******************
当您使用NSURLSession
时,它允许您执行后台下载操作。根据您发布的代码,我们不需要使用dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u LOW,0),^{}启动新线程。
请试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchFeed]; //Network activity
}
-(void)fetchFeed
{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
NSString *requestString = @"some website";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.locations = jsonObject[@"someKey"];
NSLog(@"%@", self.locations);
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}] resume];
}
这应该行得通 请查看我的答案。。它和你要找的东西差不多 我已经使用MBProgressHUD显示加载消息 简单到
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//Network activity
dispatch_async(dispatch_get_main_queue(), ^{
//do stuff after json download
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
有关更详细的答案,请查看链接
*************编辑*******************
当您使用NSURLSession
时,它允许您执行后台下载操作。根据您发布的代码,我们不需要使用dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u LOW,0),^{}启动新线程。
请试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchFeed]; //Network activity
}
-(void)fetchFeed
{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
[hud setLabelText:@"Loading..."];
NSString *requestString = @"some website";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.locations = jsonObject[@"someKey"];
NSLog(@"%@", self.locations);
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}] resume];
}
这应该行得通 使用MBProgressHUD。请注意,解析不需要时间(微秒)。所涉及的时间是网络连接时间。请使用MBProgressHUD。请注意,解析不需要时间(微秒)。所涉及的时间是网络连接时间。请使用MBProgressHUD。请注意,解析不需要时间(微秒)。所涉及的时间是网络连接时间。请使用MBProgressHUD。请注意,解析不需要时间(微秒)。所涉及的时间是网络连接时间。但是在我下载完数据之前加载就结束了。我可以从NSLog中看出。因为NSLog应该显示一些东西,但在NSLog打印任何东西之前加载页面就完成了。我需要查看您的下载代码。你实施itI的方式我编辑了我的答案。。很抱歉耽搁了。。我被抓住了,天哪,你是个救命恩人。事实上我现在正在参加一场黑客大赛。这个问题是由于线程问题造成的。但是在我完成下载数据之前加载就结束了。我可以从NSLog中看出。因为NSLog应该显示一些东西,但在NSLog打印任何东西之前加载页面就完成了。我需要查看您的下载代码。你实施itI的方式我编辑了我的答案。。很抱歉耽搁了。。我被抓住了,天哪,你是个救命恩人。事实上我现在正在参加一场黑客大赛。这个问题是由于线程问题造成的。但是在我完成下载数据之前加载就结束了。我可以从NSLog中看出。因为NSLog应该显示一些东西,但在NSLog打印任何东西之前加载页面就完成了。我需要查看您的下载代码。你实施itI的方式我编辑了我的答案。。很抱歉耽搁了。。我被抓住了,天哪,你是个救命恩人。事实上我现在正在参加一场黑客大赛。这个问题是由于线程问题造成的。但是在我完成下载数据之前加载就结束了。我可以从NSLog中看出。因为NSLog应该显示一些东西,但在NSLog打印任何东西之前加载页面就完成了。我需要查看您的下载代码。你实施itI的方式我编辑了我的答案。。很抱歉耽搁了。。我被抓住了,天哪,你是个救命恩人。事实上我现在正在参加一场黑客大赛。问题是由于线程的东西。