Question

I am using following to initialise bundle object in viewDidLoad.

documentBundle = [[NSBundle alloc] initWithPath:path];

Where path looks like following;

/Users/..../Library/Application Support/iPhone Simulator/6.1/Applications/B69B8A03-C029-4DF5-89E0-1429E73E840F/Documents/Downloads/documents.bundle

While application running I need to update documents.bundle and get rid of old one. So I remove it and download latest one from the web. I have confirmed that bundle object points to same folder but it's not able to get contents inside that bundle after I replaced existing folder. If I restart application I do get latest contents! I am not sure what's going on here. Can some one point out what could be wrong?

Following returns Nil path after I replaced bundle! I can see required file is right there from terminal!

NSString *path = [documentBundle pathForResource:filename ofType:extension];

I have also tried to reinitialise bundle object after replacing bundle but still it points to same memory address (printed using %p) and doesn't return any content from new bundle.

I get same result on both device and simulator.

Was it helpful?

Solution

It seems NSBundle remembers file system during instantiation of object. After that it doesn't recognise any new files added to that folder! If you remove folder and recreate new with same name it doesn't recognise that either. I tried NSBundle pointing to Documents directory always and used [pathForResource:filename ofType:extension inDirectory:directory] but that also doesn't work when you replace directory. In short never deal with NSBundle if your file system is dynamic using same names!

I found solution by creating current timestamp folder for each new downloads and removing older folder and then reset NSBundle object. Now it works and creates the right path as it wasn't actually exists! Read Apple documents on NSBundle for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top