olynoise
olynoise

Reputation: 2066

NSURL is nil after successfully completing NSBundleResourceRequest

I'm working with on-demand resources to access a video.

I'm successfully getting the resource, as the conditionallyBeginAccessingResourcesWithCompletionHandler returns resourcesAvailable == YES.

I can then access the URL for the resource. However, I need to access the URL on the main thread, as it is a video and needs to be presented in a UIView. When I switch to the main thread and try to access the URL, it is nil.

I've also tried passing the non-nil URL from the completion handler thread to the main thread, but still no luck accessing the video.

Here's a code example :

NSBundleResourceRequest * request = [self resourceRequest];

[request conditionallyBeginAccessingResourcesWithCompletionHandler:^(BOOL resourcesAvailable) {
    if (resourcesAvailable) {

        NSURL * url = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mov"];
        /// url is non-nil here. 

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{                       
            NSURL * urlOnMainThread = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mov"];
            //urlOnMainThread is nil!                
        }];
} else {
    NSLog(@"resources unavailable, initiating download");
    [self requestVideo];
    }
}];

Upvotes: 1

Views: 116

Answers (1)

Aleksandr Medvedev
Aleksandr Medvedev

Reputation: 9008

Your request object seems to be deallocated before you actually access data in the main thread. As per docs:

Management ends after a call to endAccessingResources() or after the resource request object is deallocated.

So you need either to retain reference to your request, or access the resource synchronously.

Upvotes: 3

Related Questions