0

I have created an iCarousel View and am trying to display a video preview in each "cell"/view.

I have the videos stored in Parse and am trying to

  1. Query from the cloud
  2. Retrieve the data from the PFFile
  3. Convert the data to URL
  4. Play URL using AVPlayer

Here my code so far.

-(void)getPast{
dataArray = [[NSMutableArray alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
PFQuery *query = [PFQuery queryWithClassName:@"History"];
[query whereKey:@"Location" containsString:[defaults objectForKey:@"location"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    pastArray = [NSMutableArray arrayWithArray:objects];
    for (PFObject *files in objects){
        PFFile *file = [files objectForKey:@"File"];
        [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

            [dataArray addObject:data];

        }];
    }
    [self.carouselView reloadData];
}];
}

Im getting an error saying that my dataArray is empty, I think the problem here could be that since I'm querying in the background, the For loop is finishing before I have received the data and therefore the array is empty, although I could be wrong and I don't know how to fix this even if I was right.

Code for displaying preview

 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {



        PFObject *object = [pastArray objectAtIndex:index];
        NSData *data = [dataArray objectAtIndex:index];
        NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSURL *URL = [NSURL URLWithString:dataString];
        NSLog(@"URL %@",URL);
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 275)];
        self.playerViewController = [[AVPlayerViewController alloc]init];
    self.playerViewController.player = [AVPlayer playerWithURL:URL];
    self.playerViewController.view.frame = view.bounds;
    self.playerViewController.showsPlaybackControls = NO;


    [view addSubview:self.playerViewController.view];
    view.autoresizesSubviews = YES;

    self.playerViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerViewController.player.currentItem];
    [self.playerViewController.player play];



    return view;


    }

How can I fix my code so that each view autoplays the PFFile video corresponding to its index in the Array.

My problems: Array is empty

Playing content for each view isn't working

Ps. Im aware that I'm not using PFObject *object.

huddie96
  • 1,240
  • 2
  • 13
  • 26

1 Answers1

0

As you guessed the for cycle finishes it's execution way before the blocks are called, you have to make sure the data is loaded before you call reloadData

The first thing that comes to my mind on how to handle this will be something like

for (PFObject *files in objects){
    PFFile *file = [files objectForKey:@"File"];
    [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)   {

        [dataArray addObject:data];
        [self checkData];
    }];
} 


- (void)checkData {
   //Check the if the data is completed
   if(dataArray.count == numberOfFiles) { //Maybe a more complex if is required here but you get the idea
       //All files are downloaded
       dispatch_async(dispatch_get_main_queue(), ^{
          //We are sure the data is ready so we reload it
          [self.carouselView reloadData];
       });
   }
}

Also you should always check if NSData is valid before loading it

Benjamin Jimenez
  • 984
  • 12
  • 26
  • How do I show the video in the preview view though? For me either I get nothing or a play button with a line through it. How can I convert the data into a URL and get each view playing its specific video? Thanks! – huddie96 Dec 16 '15 at 22:51
  • you said your problem was an empty array and now you are telling a different issue, you should ask a new question or update your current question with more information – Benjamin Jimenez Dec 17 '15 at 17:09
  • My question was "How can I fix my code so that each view autoplays the PFFile video corresponding to its index in the Array". I had two issues the array issue which I thought I knew the problem and I didn't know how to convert it into the URL that could be played in the AVPlayer – huddie96 Dec 17 '15 at 17:26