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

アプリのライブラリ ディレクトリ内にコア データ ファイルを保存するにはどうすればよいですか。

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 に答える