0

I would like to make a SVProgressHUD with a parse query findObjectsInBackgroundWithBlock. When I test, the hud is leaving 1 second after the view appears.

How can I make that the hud stay to the end of the query (I know it's a background query...) :/

What am I doing wrong ?

Any idea ?

Here is my code :

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time-consuming task
        [self loadShop];
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
    });
}

and Method :

-(void)loadShop {
    //    Configure Shop with Parse DB
    PFQuery *presentationText = [PFQuery queryWithClassName:@"presentationText"];
    [presentationText findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.presentationText.text = [[objects valueForKey:@"Text" ] objectAtIndex:0];
        }}];

    PFQuery *categoryQuery = [PFQuery queryWithClassName:@"Category"];
    [categoryQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.categoryLabel.text = [[objects valueForKey:@"Text" ] objectAtIndex:0];
            self.categoryLabel2.text = [[objects valueForKey:@"Text" ] objectAtIndex:1];
        }}];

    PFQuery *descriptionLabel = [PFQuery queryWithClassName:@"descriptionLabel"];
    [descriptionLabel findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.descriptionLabel.text = [[objects valueForKey:@"Text" ] objectAtIndex:3];
            self.descriptionLabel2.text = [[objects valueForKey:@"Text" ] objectAtIndex:0];
            self.descriptionLabel3.text = [[objects valueForKey:@"Text" ] objectAtIndex:1];
            self.descriptionLabel4.text = [[objects valueForKey:@"Text" ] objectAtIndex:2];
        }}];

    PFQuery *priceLabel = [PFQuery queryWithClassName:@"priceLabel"];
    [priceLabel findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.priceLabel.text = [[objects valueForKey:@"Text" ] objectAtIndex:0];
            self.priceLabel2.text = [[objects valueForKey:@"Text" ] objectAtIndex:1];
            self.priceLabel3.text = [[objects valueForKey:@"Text" ] objectAtIndex:2];
            self.priceLabel4.text = [[objects valueForKey:@"Text" ] objectAtIndex:3];
        }}];


    PFQuery *urlImage = [PFQuery queryWithClassName:@"urlImage"];
    [urlImage findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.imageButtonURL = [[objects valueForKey:@"URL" ] objectAtIndex:0];
            self.imageButtonURL2 = [[objects valueForKey:@"URL" ] objectAtIndex:1];
            self.imageButtonURL3 = [[objects valueForKey:@"URL" ] objectAtIndex:2];
            self.imageButtonURL4 = [[objects valueForKey:@"URL" ] objectAtIndex:3];
            imageButtonData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageButtonURL]];
            self.imageButton.imageView.image = [UIImage imageWithData: imageButtonData];

            imageButtonData2 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageButtonURL2]];
            self.imageButton2.imageView.image = [UIImage imageWithData: imageButtonData2];

            imageButtonData3 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageButtonURL3]];
            self.imageButton3.imageView.image = [UIImage imageWithData: imageButtonData3];

            imageButtonData4 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageButtonURL4]];
            self.imageButton4.imageView.image = [UIImage imageWithData: imageButtonData4];
        }}];
}
xcode_Dev
  • 237
  • 4
  • 16
  • you are using Blocks from parse. Start your SVProgressHUD before you are fetching objects. Stop it when it's finished. – Anton Mar 26 '15 at 16:59
  • @Anton It's not what I am doing here ? I start [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient]; before fetching objects : starting my method loadShop... no ? – xcode_Dev Mar 26 '15 at 17:01
  • Create separated function with your progress hud starts with all settings inside. Run it before fetch started and simply stop it when it finished – Anton Mar 26 '15 at 17:02

1 Answers1

1

you are using Blocks from parse. Start your SVProgressHUD before you are fetching objects. Stop it when it's finished.

example:

 PFQuery *priceLabel = [PFQuery queryWithClassName:@"priceLabel"];
//SVProgressHUD starts
    [priceLabel findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.priceLabel.text = [[objects valueForKey:@"Text" ] objectAtIndex:0];
            self.priceLabel2.text = [[objects valueForKey:@"Text" ] objectAtIndex:1];
            self.priceLabel3.text = [[objects valueForKey:@"Text" ] objectAtIndex:2];
            self.priceLabel4.text = [[objects valueForKey:@"Text" ] objectAtIndex:3];
//SVProgressHUD stops
        }}];

So you could start it when view will appear and stop it after last request done.

Anton
  • 3,102
  • 2
  • 28
  • 47
  • and last question (if you know, and if it's easy...), how to display progress with % on SVProgressHUD ? – xcode_Dev Mar 26 '15 at 17:09
  • You should check it on official doc on GitHub: If you'd like the HUD to reflect the progress of a task, use one of these: + (void)showProgress:(CGFloat)progress; + (void)showProgress:(CGFloat)progress status:(NSString*)status; + (void)showProgress:(CGFloat)progress status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType https://github.com/TransitApp/SVProgressHUD – Anton Mar 26 '15 at 17:13