iPhone - MBProgressHUD、Sudzc、Web 服务和 NSThread 帮助
我有以下代码,它连接到 Web 服务并返回一个类别数组。
我正在使用以下 SOAP Web 服务包装器:
..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:
..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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在执行回调的情况下使用
MBProgressHUD
,那么还有另一种方法。在开始后台进程之前初始化 HUD,
然后将以下内容放入回调方法中:
这样您就可以准确地知道 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 withinstead of
and then put the following in your callback method:
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.
不要在主线程上执行网络操作,因为这会阻塞 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 methodconnectToService
directly. TheMBProgressHUD
will detach a new thread by itself when you callshowWhileExecuting
.最终通过修改我的实用程序类解决了这个问题。我执行了两次主选择器!
Figured this out eventually by modifying my Utility class. I was performing the main selector twice!