iPhone - MBProgressHUD、Sudzc、Web 服务和 NSThread 帮助

发布于 2024-10-15 09:11:00 字数 1742 浏览 5 评论 0原文

我有以下代码,它连接到 Web 服务并返回一个类别数组。

我正在使用以下 SOAP Web 服务包装器:

http://www.sudzc.com/

..and以下 MBProgressHUD 作为活动指示器:

https://github.com/jdg/MBProgressHUD< /a>

我想要一个 HUD 进度指示器来指示它正在连接并获取结果。我目前正在使用 MBProgressHUD 来达到所需的效果,但我注意到它工作得不太正常。在加载并显示 tableView 中的实际单元格之前,进度指示器消失。我还在应用程序的不同区域使用 MBProgressHUD,但通常是每次连接到 Web 服务以获取结果时。

有人可以引导我走向正确的方向,如何修复下面的代码以使其正常工作吗?我认为这可能与显示进度指示器后触发回调方法有关。不太确定如何使用回调方法正确实现 MBProgressHUD

这是我“试图”让它发挥作用的可怕尝试。

- (void)showHUD {
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

    // Add HUD to screen.
    [self.navigationController.view addSubview:HUD];

    // Register for HUD callbacks so we can remove it from the window at the right time.
    HUD.delegate = self;

    HUD.labelText = @"Loading";

    // Show the HUD while the provided method executes in a new thread
    [HUD showWhileExecuting:@selector(runLocalNotificationHandler) onTarget:self withObject:nil animated:YES];
}

- (void)runLocalNotificationHandler {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self performSelectorOnMainThread:@selector(connectToService) withObject:nil waitUntilDone:YES];

    [pool release];
}

- (void)connectToService {
    Service1 *service = [[Service1 alloc] init];
    [service GetCategories:self action:@selector(handlerGetCategories:)];
    [service release];
}

- (void)handlerGetCategories:(id)value {
    // parse objects returned from array, populate array, reload table view data, etc
}

I have the following code, which connects to a web service and returns an array of Categories.

I am using the following wrapper for SOAP web services:

http://www.sudzc.com/

..and the following MBProgressHUD for the activity indicator:

https://github.com/jdg/MBProgressHUD

I would like to have a HUD progress indicator indicate that it's connecting and grabbing results. I'm currently using MBProgressHUD to achieve the desired effect, however I'm noticing that it's not quite working properly. The progress indicator disappears before the actual cells in my tableView are loaded and displayed. I also use the MBProgressHUD in different areas of my application, but usually every time I connect to the web service to grab results.

Could someone lead me in the right direction as to how to fix the below code to work properly? I think it may relate to the fact that the callback method is fired after the progress indicator is displayed. Not quite sure how to implement MBProgressHUD properly with a callback method.

This is my horrible attempt at "trying" to get it working.

- (void)showHUD {
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

    // Add HUD to screen.
    [self.navigationController.view addSubview:HUD];

    // Register for HUD callbacks so we can remove it from the window at the right time.
    HUD.delegate = self;

    HUD.labelText = @"Loading";

    // Show the HUD while the provided method executes in a new thread
    [HUD showWhileExecuting:@selector(runLocalNotificationHandler) onTarget:self withObject:nil animated:YES];
}

- (void)runLocalNotificationHandler {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self performSelectorOnMainThread:@selector(connectToService) withObject:nil waitUntilDone:YES];

    [pool release];
}

- (void)connectToService {
    Service1 *service = [[Service1 alloc] init];
    [service GetCategories:self action:@selector(handlerGetCategories:)];
    [service release];
}

- (void)handlerGetCategories:(id)value {
    // parse objects returned from array, populate array, reload table view data, etc
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

×纯※雪 2024-10-22 09:11:00

如果您在执行回调的情况下使用 MBProgressHUD,那么还有另一种方法。在开始后台进程之前初始化 HUD

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

然后将以下内容放入回调方法中:

[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];

这样您就可以准确地知道 HUD 何时显示和关闭。至少,它将帮助您通过查看使用新方法后发生的变化来调试真正的问题所在。

If you're using MBProgressHUD in a situation where a callback is performed, then there is another approach. Initialize the HUD before beginning your background process with

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

instead of

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

and then put the following in your callback method:

[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];

This way you know exactly when your HUD is shown and closed. At the very least, it will help you debug where the real problem is by seeing what changes after using the new approach.

ゃ人海孤独症 2024-10-22 09:11:00

不要在主线程上执行网络操作,因为这会阻塞 UI。在您的情况下,MBProgressHUD 将不会动画,甚至不会显示。

所以不要使用performSelectorOnMainThread。而是直接调用 connectToService 方法。当您调用 showWhileExecuting 时,MBProgressHUD 将自行分离一个新线程。

Don't do the network stuff on the main thread because this will block the UI. In your case the MBProgressHUD will not be animating or not even show.

So don't use performSelectorOnMainThread. Instead call the method connectToService directly. The MBProgressHUD will detach a new thread by itself when you call showWhileExecuting.

離人涙 2024-10-22 09:11:00

最终通过修改我的实用程序类解决了这个问题。我执行了两次主选择器!

Figured this out eventually by modifying my Utility class. I was performing the main selector twice!

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