在我的应用程序中,我已存储核心数据文件保存在应用程序的文档目录中 像这样
- (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;}
如何将核心数据文件存储在应用程序的Library目录中。
答案 0 :(得分: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;
}
答案 1 :(得分:1)
NSURL *libraryDirectory = [NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [[self libraryDirectory] URLByAppendingPathComponent:@"App.sqlite"];
答案 2 :(得分: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;
}
这个完整的方法解决了我的问题,谢谢你的帮助。