调用 Web 服务时对 ActivityIndicator 使用 PerformSelector 的问题 - IOS
我试图让 PerformSelector 在进行 Web 服务调用时在单独的线程上加载活动指示器。问题是“return parsedData;”未在 fetchJSON: 中设置。但是,当我在 getData: 方法中打印 parsedData 时,它会正常返回。我假设 return 是在 PerformSelector 完成获取数据之前执行的。有没有办法让 fetchJSON: 方法在返回 parsedData 之前等待 PerformSelector 完成?
-(void)showActivityIndicator
{
CGRect frame = CGRectMake(0.0, 0.0, 125.0, 125.0);
loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[loading setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loading hidesWhenStopped];
//loading.center=[self tableView].center;
[loading startAnimating];
[loading sizeToFit];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
// initing the bar button
//UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:loading];
//loadingView.target = self;
[loadingView addSubview:loading];
}
- (NSDictionary *)fetchJSON:(NSString *)urlString
{
NSMutableString *domain = [[NSMutableString alloc] initWithString:@"http://www.blablabla.com/dev/"];
[domain appendString:urlString];
//NSLog(@"%@", domain);
NSURL *url = [NSURL URLWithString:domain];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self showActivityIndicator];
[self performSelector:@selector(getData:) withObject:req afterDelay:0.0];
//[self performSelectorOnMainThread:@selector(getData:) withObject:req waitUntilDone:YES];
return parsedData;
}
-(IBAction)getData:(id)sender
{
NSURLResponse* response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:sender returningResponse:&response error:nil];
parsedData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(@"GET DATA %@", parsedData);
[loading stopAnimating];
loading = nil;
}
I'm trying to get the performSelector to load the activity indicator on a separate thread while the web service call is made. The issue is the "return parsedData;" is not being set in fetchJSON:. However, when I print the parsedData in getData: method, it's coming back fine. I assume the return is being executed before the performSelector is finished getting the data. Is there any way to have the fetchJSON: method wait for performSelector to finish before returning parsedData?
-(void)showActivityIndicator
{
CGRect frame = CGRectMake(0.0, 0.0, 125.0, 125.0);
loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[loading setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loading hidesWhenStopped];
//loading.center=[self tableView].center;
[loading startAnimating];
[loading sizeToFit];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
// initing the bar button
//UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:loading];
//loadingView.target = self;
[loadingView addSubview:loading];
}
- (NSDictionary *)fetchJSON:(NSString *)urlString
{
NSMutableString *domain = [[NSMutableString alloc] initWithString:@"http://www.blablabla.com/dev/"];
[domain appendString:urlString];
//NSLog(@"%@", domain);
NSURL *url = [NSURL URLWithString:domain];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self showActivityIndicator];
[self performSelector:@selector(getData:) withObject:req afterDelay:0.0];
//[self performSelectorOnMainThread:@selector(getData:) withObject:req waitUntilDone:YES];
return parsedData;
}
-(IBAction)getData:(id)sender
{
NSURLResponse* response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:sender returningResponse:&response error:nil];
parsedData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(@"GET DATA %@", parsedData);
[loading stopAnimating];
loading = nil;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“performSelector 在单独的线程上加载活动指示器”
??
一般来说,UIKit 不是线程安全的。
在注释掉的代码和令人困惑的方法声明之间(为什么 getData: an IBAction?它是从代码内部调用还是作为控件的操作调用?),我真的缺少有关您在这里尝试执行的操作的上下文。
您能否首先对您想要完成的任务以及该代码如何适应该情况进行高级概述?
"performSelector to load the activity indicator on a separate thread"
??
UIKit is, in general, not thread safe.
Between the commented out code and confusing method declarations (why is getData: an IBAction? Is it called both from within code and as a control's action?), I'm really missing context on what you are trying to do here.
Could you start by just giving a high-level overview of what you're trying to accomplish and how this code fits into that picture?