1

In my Application i have stored The core data file is saved in the app's Document directory like this

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{

if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"App.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return _persistentStoreCoordinator;}

How can i store the core data file inside the app's Library directory.

4

3 回答 3

2
-(NSString*)applicationLibraryDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
     NSError *error=nil;
    NSString *storePath = [[self applicationLibraryDirectory] stringByAppendingPathComponent: @"sample.sqlite"];
    NSURL *storeUrl =[NSURL fileURLWithPath:storePath];
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
    {
        NSLog(@"store url %@",storeUrl);
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);
    }    

    return persistentStoreCoordinator;
}
于 2013-09-30T11:56:00.047 回答
1
NSURL *libraryDirectory = [NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [[self libraryDirectory] URLByAppendingPathComponent:@"App.sqlite"];
于 2013-09-30T11:56:45.100 回答
0
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{
NSPersistentStoreCoordinator *persistentStoreCoordinator;
if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}
NSError *error=nil;
NSString *storePath = [[self applicationLibraryDirectory] stringByAppendingPathComponent: @"Arivanza.sqlite"];
NSURL *storeUrl =[NSURL fileURLWithPath:storePath];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
    NSLog(@"store url %@",storeUrl);
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}

return persistentStoreCoordinator;
}

这个完整的方法解决了我的问题,感谢您的帮助。

于 2013-12-26T07:10:45.823 回答