SVProgressHUD与查询findObjectsInBackgroundWithBlock?

时间:2015-03-26 16:56:12

标签: parse-platform hud svprogresshud

我想使用解析查询 SVProgressHUD制作findObjectsInBackgroundWithBlock。 当我测试时,视图出现后,hud会在1秒后离开。

如何让 hud 保持到查询结尾(我知道它是后台查询...):/

我做错了什么?

有什么想法吗?

这是我的代码:

-(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];
            });
    });
}

和方法:

-(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];
        }}];
}

1 个答案:

答案 0 :(得分:1)

您正在使用解析块。在获取对象之前启动SVProgressHUD。它完成后停止。

示例:

 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
        }}];

因此,您可以在显示视图时启动它,并在上次请求完成后停止它。