2013-09-30 58 views
1

在我的应用程序都存储在核心数据文件保存在应用程序的文件目录 这样核心数据文件保存应用程序的库目录

- (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;} 

我怎么能核心数据文件存储里面的内应用程序的库目录。

回答

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; 
} 
+0

它显示一个错误,如没有已知的类选择器'applicationLibraryDirectory'方法 –

+0

更改静态方法,我编辑。请看看。 – karthika

1
NSURL *libraryDirectory = [NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; 
NSURL *storeURL = [[self libraryDirectory] URLByAppendingPathComponent:@"App.sqlite"]; 
+0

它显示了一些关于不兼容指针转换的错误和警告。 –

+0

使用'lastObject'修正 – Abizern

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; 
} 

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