Search code examples
iostvoson-demand-resources

Is there some time limit with perform segue?


Is there some time limit when a segue is triggered and the code is being executed inside prepareForSegue:sender:?

I mean this, suppose I am inside I am triggering a AVPlayerViewController but this controller requires a video that must be download asynchronously so, inside prepareForSegue:sender: I have a code like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

  AVPlayerVC *playerViewController = (AVPlayerVC *)[segue destinationViewController];

  [self.onDemandResources loadResourcesWithTags:@[tag]
                                  runOnCompletion:^{
                                      NSString *pathBundle = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
              NSURL *videoURL = [NSURL fileURLWithPath:pathBundle];
              AVPlayer *player = [AVPlayer playerWithURL:videoURL];
              playerViewController.player = player;

              [player play];
}];
}

The problem I am having is the [segue destinationViewController] being presented but the video never playing. The impression I have is that the code takes to long to execute and the segue is already presented.

Any ideas?


Solution

  • It is difficult to give you a complete and working solution, since you only provided the prepareForSegue(_:) function. I don't know the details of the source view controller and the destination view controller. Hereby my five cents.

    The problem with blocks is that they often run in a different queue and the prepare for segue therefore fires the block, doesn't wait for the block to complete, but segues to the next view controller.

    What I see in your code is that the old view controller that has given control to the new view controller is playing your movie file. Just having your player in the new controller pointing to the same player in the old view controller doesn't display the movie on screen. You need an AVPlayerView to display your movie on screen.

    Since I'm not familiar with the message "self.onDemandResources loadResourcesWithTags:@[tag]", I can't comment on this message with block. However, instead of saying:

    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    playerViewController.player = player;
    

    I would suggest the following code in the block:

    playerViewController.videoURL = videoURL;
    

    And run the line:

    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    

    in the destination view controller "AVPlayerVC *playerViewController".

    Hope this helps.