MBProgressHUD showAnimated: whileExecutingBlock: not working - ios

I'm using a method for scanning Bluetooth device, which I import from another framework. The scanning method would take a while, and it'll block the GUI, which is what we never want to happen.
I'm also having MBProgressHud, trying to show a hud while scanning, but it's not working (hud not showing up). Any help?
Here's the code I'm currently using:
[hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = [Util scanBT];
}];
Edit 1: Okay, so if I use this code, it'll still block my UI for a while, then all suddenly continue to run.
hud = [[MBProgressHUD alloc] initWithView:self.view];
hud.labelText = #"Now scanning";
hud.dimBackground = YES;
hud.opacity = 0.5;
[hud show:YES];
[hud hide:YES afterDelay:5.0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
self.btDevices = [Util scanBT];
});
Edit 2: Ok, I will post all my block of code:
hud = [[MBProgressHUD alloc] initWithView:[self getTopView:self.view]];
hud.labelText = #"Now scanning";
hud.dimBackground = YES;
hud.opacity = 0.5;
[hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = [Util scanBT];
}];
dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{
//Whatever is happening in the BT scanning method will now happen in the background
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:[self getTopView:self.view] animated:YES];
});
});
/** Recursion to get the top most view in view layer. */
- (UIView *)getTopView:(UIView *)view
{
if ([view.superview class]) {
return [self getTopView:view.superview];
}
return view;
}
I'm request scanning bt in a popover, but I want to show HUD in a main View, so I write a block to retrieve the main view. Maybe that's where the problem occur?

Try this:
In your viewDidload or the method where you want to place it
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[hud setDimBackground:YES];
[hud setOpacity:0.5f];
[hud show:YES];
[hud hide:YES afterDelay:5.0];
[self performSelector:#selector(startScanning) withObject:nil afterDelay:5.0];
And your method
- (void) startScanning {
self.btDevices = [Util scanBT];
}
OR I think you should try it running
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
// Insert task code here
self.btDevices = [Util scanBT];
});
Try it with completion block
[hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = [Util scanBT];
} completionBlock:^{
//code for after completion
}];
OR you can also try this
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{
//Whatever is happening in the BT scanning method will now happen in the background
self.btDevices = [Util scanBT];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});

Related

Update progress of MBProgressHUD

I have a scenario in which my app downloads a large chunk of data only for the first time and this happens in say ViewController1. I'm using a different class to download the data and another one to save the data. So my question is, how can I update the progress of MBProgressHUD object created in ViewController1 to display the progress to the user?
The approach that I've adopted is to use NSNotificationCenter to send notifications. I'm sending notifications at the end of methods (13) in the class that saves data.
Here's what I've been doing:
//ViewController1.h
#interface ViewController1 ()
{
MBProgressHUD *hud;
float progress;
}
#end
//ViewController1.m
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"downloadComplete"
object:nil];
}
- (IBAction)sumitButtonDidClick:(id)sender {
hud = [[MBProgressHUD alloc] init];
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Please wait...", #"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);
[hud showAnimated:YES];
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
progress = 0.0f;
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
DownloadClass * obj1 = [[DownloadClass alloc] init];
[obj1 downloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
}
- (void) receiveNotification:(NSNotification *) notification {
if ([[notification name] isEqualToString:#"downloadComplete"])
{
NSLog (#"Successfully received the download complete notification!");
progress += 7.7f;
//[hud setProgress:progress]; // won't work
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD HUDForView:self.view].progress = progress;
});
}
}
Update: I'm receiving the notifications posted by the class that saves
data
You have created a instance variable for MBProgressHUD as MBProgressHUD *hud; and you start progress on a sumitButtonDidClick method as below :
hud = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, -30, 320, 768) ];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Please wait...", #"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);
UIWindow *window=[[[UIApplication sharedApplication]delegate]window];
hud.center=window.center;
[window addSubview:hud];
[hud showAnimated:YES];
But increasing progress as Class method of MBProgressHUD as [MBProgressHUD HUDForView:self.view].progress = progress;.
Fix for the issue is below :
dispatch_async(dispatch_get_main_queue(), ^{
hud.progress = progress;
});
if you want to hide the progress you can use
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
According to the documentation for MBProgressHUD you appear to be creating the HUD incorrectly. The example they give, is as follows...
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// Set the bar determinate mode to show task progress.
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Loading...", #"HUD loading title");
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
You are creating an instance and attaching it to UIWindow
The official documentation lists it like this:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.label.text = #"Loading";
NSProgress *progress = [self doSomethingInBackgroundCompletion:^{
[hud hideAnimated:YES];
}];
hud.progressObject = progress;
You can find it here: https://github.com/jdg/MBProgressHUD

MBProgressHUD is displayed only when the operation is finished

i'm using MBProgressHUD to show a popup with a loading animation but i have a problem. The progress indicator call is executed when i press a button. This is the action
- (IBAction)calendarioButtonPressed:(UIButton *)sender {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Uploading";
[hud show:YES];
[self getCalendarioJson];
}
in the getCalendarioJson i have this
- (void) getCalendarioJson {
//LETTURA CALENDARIO - INIZIO
NSString *calLink = myLink;
NSData* responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:calLink]];
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions error:nil];
NSDictionary *firstObject = [json objectAtIndex:0];
NSDictionary *cal = [firstObject objectForKey:#"output"];
variabiliGlobali.calendario = [[NSMutableArray alloc] init];
for (NSDictionary *dict in cal) {
[variabiliGlobali.calendario addObject: dict];
}
//LETTURA CALENDARIO - FINE
}
Why does the loading popup appear only at the end of getCalendarioJson execution?
The button have a segue. When i go back from the target view i can see the popup.
What is the metter? If i delete the segue i can see the popup at the end of getCalendarioJson execution (because there is not a segue).
Thanks in advance.
You have a method [self getCalendarioJson]; but this method run in the main thread, the main thread is for UI updates, when you download data or make a operation with several seconds of duration, the main thread wait for make the UI update.
For solve this situation, put the method in a background thread, can you use [self performSelectorInBackground:#selector(getCalendarioJson) withObject:nil];
From https://github.com/jdg/MBProgressHUD
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = #"Loading";
[self doSomethingInBackgroundWithProgressCallback:^(float progress) {
hud.progress = progress; //Here you set the progress
} completionCallback:^{
[hud hide:YES];
}];
You should set the progress value.

How to add MBProgressHUD waiting loading data in iOS

I have a problem: I used AFNetworking to get data from server, i used NSOperationQueue to add many operation to it, in each request, I added this operation to queue and used waitUntilAllOperationsAreFinished as bellow :
request 1
...
[queue addOperation:operation1];
[queue waitUntilAllOperationsAreFinished];
request 2
...
[queue addOperation:operation2];
[queue waitUntilAllOperationsAreFinished];
I tried above code and my programs seems hangs and after that, it runs ok.So that I want to added MBProgressHUD to waiting queue finish, then I want to check if queue finish, I want to hide MBProgressHUD. But when i click on Button to load UIViewController, MBProgressHUD cannot show.
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
Actually, I want to show MBProgressHUD when queue finish. How can i do that? Thanks all
Shortly you can do it like this:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];
Check MBProgressHUD's usage
Another Better Approach..
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = #"Doing funky stuff...";
HUD.detailsLabelText = #"Just relax";
HUD.mode = MBProgressHUDModeAnnularDeterminate;
[self.view addSubview:HUD];
[HUD showWhileExecuting:#selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];
And doSomeFunkyStuff
- (void)doSomeFunkyStuff {
float progress = 0.0;
while (progress < 1.0) {
progress += 0.01;
HUD.progress = progress;
usleep(50000);
}
}
Detail answer is here..
waitUntilAllOperationsAreFinished will bock the current thread, which is probably the main thread so you really don't want to do that.
If you're using AFNetworking then check out this method on AFHTTPClient
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
So show you HUD then call this method and hide your HUD in the completionBlock

Uiactivityindicator not showing in subview

I am trying to show a spinner in a subview on a tableview controller on my main thread. The view is only showing on occasions and generally appears to be showing after my background thread has finished. Here is my code:
[self.view addSubview:spinnerView];
[self.view bringSubviewToFront:spinnerView];
self.spinnerView.layer.cornerRadius = 8;
self.spinnerView.center = CGPointMake(160, 150);
[spinner startAnimating];
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^ {
dispatch_async(dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:10];
// Add recurring transactions if necessary
if (tran.recur.id != 0) {
[Utilities addRecurringTransactionsUpToCurrentMonthByTransaction:tran];
}
[spinner stopAnimating];
[self dismissViewControllerAnimated:YES completion:nil];
});
});
dispatch_release(downloadQueue);
}
}
Since all UI is running on main thread, you must call performSelectorOnMainThread:withObject:waitUntilDone: like below:
[self.view addSubview:spinnerView];
[self.view bringSubviewToFront:spinnerView];
self.spinnerView.layer.cornerRadius = 8;
self.spinnerView.center = CGPointMake(160, 150);
[spinner performSelectorOnMainThread:#selector(startAnimating) withObject:nil waitUntilDone:NO];
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^ {
dispatch_async(dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:10];
// Add recurring transactions if necessary
if (tran.recur.id != 0) {
[Utilities addRecurringTransactionsUpToCurrentMonthByTransaction:tran];
}
[spinner performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:NO];
[self dismissViewControllerAnimated:YES completion:nil];
});
});
dispatch_release(downloadQueue);

dispatch_async and frame rate

I'm trying to learn GCD, so I don't have a full grasp on how it works yet. For some reason, I experience a permanent drop in frame rate after I call the following method. If I don't use the dispatch functions and simply write the data on the main loop, the frame rate stays at 60. I don't know why.
-(void)saveDataFile {
_hud = [MBProgressHUD showHUDAddedTo:self.parentView animated:YES];
_hud.labelText = NSLocalizedString(#"Saving data...", nil);
dispatch_queue_t myQueue = dispatch_queue_create("myQueueName", NULL);
dispatch_async(myQueue, ^(void) {
#autoreleasepool {
id data = [self.model getData];
if (data != nil) {
NSString *filePath = #"myPath";
[data writeToFile:filePath atomically:YES];
}
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
[_hud hide:YES];
});
});
}
Solved. I followed the implementation of HUD from this question: MBProgressHUD not showing
Basically, I need to remove the HUD rather than simply hide it. Otherwise the HUD animation continued, invisible to me, hence causing the drop in frame rate.
-(void)saveDataFile {
// create HUD and add to view
MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.parentView];
hud.labelText = NSLocalizedString(#"Saving data...", nil);
hud.delegate = self;
[self.parentView addSubview:hud];
// define block for saving data
void (^saveData)() = ^() {
#autoreleasepool {
id data = [self.model getData];
if (data != nil) {
NSString *filePath = #"myPath";
[data writeToFile:filePath atomically:YES];
}
}
}
// use HUD convenience method to run block on a background thread
[hud showAnimated:YES whileExecutingBlock:saveData];
}
// remove hud when done!
//
- (void)hudWasHidden:(MBProgressHUD *)hud {
[hud removeFromSuperview];
}

Resources