I'm trying to add a sqlite persistent store in Library/Application-Support
folder as shown below:
- (NSManagedObjectModel *)managedObjectModel
{
if (!_managedObjectModel)
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"delete" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
}
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (!_persistentStoreCoordinator)
{
@synchronized(self)
{
if (!_persistentStoreCoordinator)
{
NSError *error = nil;
NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject] @"delete.sqlite"];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSPersistentStore * persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
_persistentStoreCoordinator = persistentStoreCoordinator;
}
}
}
return _persistentStoreCoordinator;
}
This fails with the following error:
CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null)
URL:file:///Users/harshithg/Library/Developer/CoreSimulator/Devices/D9A32558-558A-4E0E-915D-FEF25C772669/data/Containers/Data/Application/9517BBBB-E383-467E-B0E2-8960B734B239/Library/Application%20Support/delete.sqlite
options:(null) ...
returned error NSCocoaErrorDomain(512) with userInfo dictionary { reason = "Failed to create file; code = 2";
I'm on iOS 13.3 and Xcode 11.3. Does anyone know what's happening here?
The Application Support folder does not exist unless you explicitly create it. None of your code creates it. There's an NSFileManager method that will give you the NSApplicationSupportDirectory URL and create it (if it doesn't already exist) at the same time.