pathForResource returns nil

Hi everyone,


I'm working on an app that exchanges data with a server, and the mobile device has an option to wipe the database clean.

I manage to delete the .sqlite, but I cannot copy the fresh one.

I keep getting a "nil" for the NSString *param variable, and I can't understand why. Of course the following copyItemAtURL fails.


Besides, the whole code block is inside a try/catch, but the code inside the catch is not being executed.

Any clue? Code is as follows:

NSError* err = nil;

NSBundle *bundle = [NSBundle mainBundle];

NSString *path = @"BaseScope";

NSString *type = @"sqlite";

NSString *param = [bundle pathForResource:path ofType:type];

if (param != nil)

{

  NSURL *preloadURL = [NSURL fileURLWithPath:param isDirectory:false];

  if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])

  {

     //some code

  }

}

This may mean you've placed the sqlite file at the wrong location within the bundle, but I assume you've carefully checked this already.


If this is an iOS app, remember that the iOS file system is case sensitive, so check that that "BaseScope" is capitalized exactly as in your built bundle.


A couple of other observations:


— The first parameter of 'pathForResource' is a file name, not a path. You're passing something that works as a file name, but since you call the variable "path", I wonder if you're passing a subdirectory name by mistake, and assuming it'll find a sqlite file in that subdirectory.


— There's no point in using 'pathForResource' if you're immediately going to convert the result to a URL. Use one of the 'URLForResource:' methods instead.


Note that it's preferable to use URLs instead of paths generally. And it would save you effort in this case.

Your assumption might be wrong as well 😀

I come from the .NET framework, developing in web environment.

The iOS + mobile combo is something totally new to me, and the Objective C syntax doesn't really help 😀 , I don't really know where the cursed sqlite is, provided it is ACTUALLY somewhere (I started suspecting it's never been there in the first place).



We (coworkers and me) "solved" the problem pasting some code copied from a tutorial, and it seems to work just fine.

Still, there are several things that work, and I can't understand WHY or HOW they work, like what the **** is in the bundle, how to put stuff inside it so that the user "gets" said stuff. Too bad customer is in a hurry, company won't hire apple specialist and I was the designated victim 😀


That said, thanks for your observations, they're definitely useful and I'll try to mind them in the future.

Happy coding \o/

If it makes you feel any better, I had the same problem writing .NET-based code in an Azure server environment. I had a simple data file I wanted to load whenever the service was loaded, but I never really figured out how to get to it.


FWIW, a bundle is just a directory with a preset subdirectory structure. One of the subdirectories is called "Resources", and that's where 'pathForResource' starts looking for files. ("Starts" because resources may be localized, so it will also look in subdirectories for the current locale.)


If you add a file to your Xcode project (that's not a source file or anything it has special knowledge of) Xcode assumes it's a resource of some kind, and arranges to copy it to the Resources subdirectory of the bundle built for its target.


This document might help you get yourself better located in the Apple universe:


developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Bundle.html


and it would lead you to this document about bundles:


developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html


That's likely more detailed than you want, but it's still short enough to read for general orientation in a leisurely lunchtime.

Ahah yes, that made me feel better 😀


At least now I know I'm not alone 😀

Thanks (again) for the useful informations and the yummy links: I'll make sure to bookmark them in my "Apple" folder 🙂


Hope you have a nice coding day 🙂

cu!

pathForResource returns nil
 
 
Q