Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用MPProgressHUD和NSSession加载数据同步_Objective C_Ios7_Objective C Blocks - Fatal编程技术网

Objective c 使用MPProgressHUD和NSSession加载数据同步

Objective c 使用MPProgressHUD和NSSession加载数据同步,objective-c,ios7,objective-c-blocks,Objective C,Ios7,Objective C Blocks,我有一个异步加载数据的方法 -(void)loadingDataAsynchronously{ NSURL *url = [NSURL URLWithString:@"http://vbahrain.azurewebsites.net/api/yearapi"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; NSURLSession *session = [NSURLSession

我有一个异步加载数据的方法

-(void)loadingDataAsynchronously{

    NSURL *url = [NSURL URLWithString:@"http://vbahrain.azurewebsites.net/api/yearapi"];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];


    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDownloadTask  *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

        NSData *data = [[NSData alloc] initWithContentsOfURL:location];


        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

        self.yearBucket = [NSMutableArray array];

        for (NSDictionary * dict in array) {

            Year *year = [[Year alloc ]init];

            year.yearName =[dict objectForKey:@"Year"];
            year.speeches = [dict objectForKey:@"Speeches"];

            [self.yearBucket addObject:year];


        }

        dispatch_async(dispatch_get_main_queue(), ^{


            [self.tableView reloadData];

        });



    }];
    [task resume];
}
这是我用来加载MPProgressHUD的代码

 - (void)viewDidLoad
{
    [super viewDidLoad];

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

    HUD.mode = MBProgressHUDModeDeterminate;
    HUD.labelText = @"Loading";
    HUD.delegate = self;


    [HUD showWhileExecuting:@selector(loadingDataAsynchronously) onTarget:self withObject:Nil animated:YES];


}

MPProgressHUD在一秒钟内出现并消失。如何显示MPProgressHUD以在后台线程中显示加载数据的实际进度。

您可以尝试在
异步加载数据方法中删除HUD

 - (void)viewDidLoad
{
    [super viewDidLoad];

   [MBProgressHUD showHUDAddedTo:self.view animated:YES];
   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
       [self loadingDataAsynchronously];
   });


}

-(void)loadingDataAsynchronously{

    NSURL *url = [NSURL URLWithString:@"http://vbahrain.azurewebsites.net/api/yearapi"];

    ...

    dispatch_async(dispatch_get_main_queue(), ^{

        [self.tableView reloadData];

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

    });

    ...

}

请查看编辑。我以前试过这样做,但没用。因此,我试图在MPProgressHUD类中使用不同的方法,即使这样似乎也不起作用。