调用 Web 服务时对 ActivityIndi​​cator 使用 PerformSelector 的问题 - IOS

发布于 2025-01-01 17:40:49 字数 2118 浏览 1 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2025-01-08 17:40:49

“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文