Configure SQLite store URL #216

maikg opened this Issue Jul 21, 2012 · 10 comments

5 participants


I'd like to configure the URL where the SQLite store is created during the setup phase, which, as far as I can see, is currently not possible. In my case, I want to store the store file in the ~/Library/Caches folder, since I'm using Core Data as a local cache for a web service. Ideally, I'd fix this without resorting to hacks like overriding MR_urlForStoreName: through a category or method swizzling. What would be the best way to go about this?

I would probably introduce a new setting in the Options category of MagicalRecord that takes an NSURL instance. The implementation of MR_urlForStoreName: could then simply return this particular NSURL instance if it is provided, and resort to the default implementation otherwise. I personally think this is the best approach, but I thought I'd run it by you first to see if there's any better way.

We could consider allowing a block to be specified instead of a static NSURL instance to provide more room for custom logic, but that seems like overkill in this case.


You can do it if you provide an NSURL instance instead of a NSString. This is really dirty since it violates type safety. But it works since the file URL is determined like this:

NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];

MagicalRecord should definitely provide a way to use an URL instead of a string.

Magical Panda Software member

@casademora I see your point. But like maikg, I really need to specify the exact URL because the SQLite file may be:

  • in a subfolder of the documents / app support directory
  • in the caches directory
  • somewhere in the test bundle for unit testing

In all of these cases the file would not be found and an empty file would be created in the app support directory. I would recommend moving away from string based methods and instead introduce NSURL based methods. I can see Apple doing this as well. We could have e.g.

[MagicalRecord setupCoreDataStackWithStore:storeURL]

What do you think?


@casademora Of course we could also use a string to specify the path. But the current implementation only searches the documents directory and the application support directory for the store file. When I use a NSURL instead, it skips the search and uses the NSURL directly (Line 67 in NSPersistentStoreCoordinator+MagicalRecord.m). That's what I need, I want to be able to specify the exact location instead of the plain file name. That's why I currently use a NSURL as a workaround. But that's clearly not as designed.
If you agree with the idea of introducing URL based methods I would implement that and make a pull request. Please let me know what you think.

Magical Panda Software member

Yes, that's actually one use case of mine. For me the super important use is unit testing. My application works with sqlite files and I want to run unit tests with test data that comes from a sqlite file that resides inside the test bundle. Without being able to specify that the store file is in the test bundle, it's not possible to successfully run the tests.


@lightforce, I'd love to see a some URL based methods introduced. They're exactly what I need in my case.


@stephendavis89 Have a look at issue 51: #51
You can find my fork here:
I'd like to make a pull request, but I'm still waiting for a response of @casademora


@lightforce Cool. Thanks!


This has been addressed in the upcoming MagicalRecord 3. Checkout the release/3.0 branch if you'd like to try it out.

@tonyarnold tonyarnold closed this Dec 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment