0

我在我的应用程序中使用核心数据,我想在下次更新时启动 iTunes 文件共享,但需要先移动我的应用程序 sqlite 数据库。我已尝试使用下面的代码,但应用程序在启动时崩溃。我想我可以在 NSPersistentStoreCoordinator 中用新的商店 url 替换旧的商店 url,其中“newDatabasePath”与新的商店 url 匹配。

然后将sqlite文件替换为

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//check app has run before and has a current db
if ([saveData boolForKey:@"hasRunBefore"]) {
NSString *oldDatabasePath = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:@"AppData.sqlite"];
        NSString *newDatabasePath = [privateDocsPath stringByAppendingPathComponent:@"AppData.sqlite"];
        NSError *error;
        if ([fileMgr fileExistsAtPath:newDatabasePath]) {

            [fileMgr removeItemAtPath:newDatabasePath error:&error];
            [fileMgr copyItemAtPath:oldDatabasePath toPath:newDatabasePath  error:&error];
        }

        [fileMgr removeItemAtPath:oldDatabasePath error:&error];

        BOOL databaseMoved = YES;
        [saveData setBool:databaseMoved forKey:@"databaseMoved"];
}
}

谢谢

在阅读了一个类似的问题后,我尝试了一种新的方法来解决这个问题并取得了一些成功。我尝试像这样重置coredata堆栈

- (void)resetDatabase {

NSPersistentStore* store = [[__persistentStoreCoordinator persistentStores] lastObject];

NSError *error = nil;
NSURL *storeURL = store.URL;

// release context and model
__managedObjectModel = nil;
__managedObjectContext = nil;

//[__persistentStoreCoordinator removePersistentStore:store error:nil];

__persistentStoreCoordinator = nil;

NSFileManager* fileMgr = [NSFileManager defaultManager];
[fileMgr removeItemAtPath:storeURL.path error:&error];
if (error) {
    NSLog(@"filemanager error %@", error);
}

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//create Private Documents Folder
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libDir = [libPaths objectAtIndex:0];
NSString *privateDocsPath = [libDir stringByAppendingPathComponent:@"Private Documents"];
if (![fileMgr fileExistsAtPath:privateDocsPath])
    [fileMgr createDirectoryAtPath:privateDocsPath withIntermediateDirectories:YES attributes:nil error:nil];


NSString *oldDatabasePath = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:@"AppData.sqlite"];
NSString *newDatabasePath = [privateDocsPath stringByAppendingPathComponent:@"AppData.sqlite"];

BOOL removedItemAtPath = NO;
BOOL copiedItemToPath = NO;

if ([fileMgr fileExistsAtPath:newDatabasePath]) {
    DLog(@"DATABASE EXISTS AT PATH");
    removedItemAtPath = [fileMgr removeItemAtPath:newDatabasePath error:&error];
    if (removedItemAtPath) {
        DLog(@"ITEM REMOVED");
    }
    else
        DLog(@"FAILED TO REMOVE ITEM: %@", error);

    copiedItemToPath = [fileMgr copyItemAtPath:oldDatabasePath toPath:newDatabasePath  error:&error];
    if (copiedItemToPath) {
        DLog(@"ITEM COPIED");
    }
    else
        DLog(@"FAILED TO COPY ITEM: %@", error);
}

// recreate the stack
__managedObjectContext = [self managedObjectContext];

}

使用这种方法,当我第一次启动它时尝试从 coredata 堆栈加载数据时,应用程序仍然会引发异常,但随后会重新加载一切正常,并使用“库/私人文档”中新位置的 sqlite 文件

4

3 回答 3

0

答案竟然像往常一样简单!

我只是将复制应用程序原始数据库所需的代码移动到 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 在添加商店之前,而不是尝试从 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions .

又浪费了一天!

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

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

//test if app already run before, copy database over and remove old one, finally switch store url to new directory
if ([saveData boolForKey:@"hasRunBefore"]) {

    if ([saveData boolForKey:@"databaseUpdated"] != YES) {

        DLog(@"MOVING DATABASE");
        NSFileManager* fileMgr = [[NSFileManager alloc] init];
        fileMgr.delegate = self;
        NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docsDir = [dirPaths objectAtIndex:0];
        //create Private Documents Folder
        NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *libDir = [libPaths objectAtIndex:0];
        NSString *privateDocsPath = [libDir stringByAppendingPathComponent:@"Private Documents"];
        if (![fileMgr fileExistsAtPath:privateDocsPath])
            [fileMgr createDirectoryAtPath:privateDocsPath withIntermediateDirectories:YES attributes:nil error:nil];

        NSString *oldDatabasePath = [docsDir stringByAppendingPathComponent:@"AppData.sqlite"];
        NSString *newDatabasePath = [privateDocsPath stringByAppendingPathComponent:@"AppData.sqlite"];
        NSError *error;
        //BOOL removedItemAtPath = NO;
        BOOL copiedItemToPath = NO;

        copiedItemToPath = [fileMgr copyItemAtPath:oldDatabasePath toPath:newDatabasePath  error:&error];
        if (copiedItemToPath) {
            DLog(@"ITEM COPIED");
        }
        else
            DLog(@"FAILED TO COPY ITEM: %@", error);

        [fileMgr removeItemAtPath:oldDatabasePath error:&error];

        [saveData setBool:YES forKey:@"databaseUpdated"];
        [saveData synchronize];

    }

}


//NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppData.sqlite"];
NSURL *storeURL = [[self applicationHiddenDocumentsDirectory] URLByAppendingPathComponent:@"AppData.sqlite"];

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
    DLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return __persistentStoreCoordinator;
}
于 2013-08-03T07:55:25.980 回答
0

既然您说错误消息是:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack:

这非常强烈地指向这一行:

NSString *oldDatabasePath = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:@"AppData.sqlite"];

错误消息也告诉您原因 - 您在空数组上使用索引 0。所以,dirPaths是空的。我不知道为什么,因为你没有发布你给它一个值的代码,但这就是这次崩溃的原因。

于 2013-08-02T16:13:12.197 回答
0

我遇到了同样的问题,并找到了一个非常简单的解决方案。基本上,在初始化 NSPersistentStoreCoordinator 之前,就像在顶部一样- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,将数据库文件从当前位置移动到新位置。如果新位置尚不存在,请确保创建新位置。在下面的示例代码中,我使用Library/Application Support目录下的Database子目录。我没有显示“数据库”目录的创建;但是,这也必须在移动文件之前发生。确保您的商店初始化使用新位置。

NSURL *storeURL = [NSURL fileURLWithPath:[self getFullPrivatePath:@"<Database file name>"]];



这里有一些代码可以帮助说明这一点。我也包含了我的辅助方法,因此对于这些方法的来源或它们的工作方式没有任何混淆。请注意,必须移动 3 个数据库文件,这就是存在循环的原因。它们是[数据库文件名][数据库文件名]-wal[数据库文件名]-shm

- (void)convertPublicFilesToPrivate
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Move the database files
    NSArray<NSString *> *dbFiles = [self filesByPrefix:@"<database file name>" isPublic: YES];
    for (NSString *dbFile in dbFiles) {
        NSString *fileName = [dbFile lastPathComponent];
        NSError *error;
        NSString *privatePath = [self getFullPrivatePath:fileName];
        [fileManager moveItemAtPath: dbFile toPath: privatePath error:&error];
        NSLog(@"Move database file %@ returned %@", fileName, error);
    }
}

-(NSString *)getPublicPath
{
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}

- (NSString *)getPrivatePath
{
    return [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"Database"];
}

-(NSString*)getFullPublicPath:(NSString *)fileName
{
    NSString *documentsDirectory = [self getPublicPath];
    if (fileName != nil) {
        return [documentsDirectory stringByAppendingPathComponent:fileName];
    } else {
        return documentsDirectory;
    }
}

-(NSString*)getFullPrivatePath:(NSString *)fileName
{
    NSString *documentsDirectory = [self getPrivatePath];
    if (fileName != nil) {
        return [documentsDirectory stringByAppendingPathComponent:fileName];
    } else {
        return documentsDirectory;
    }
}

- (NSArray*)filesByPrefix:(NSString*)prefix isPublic:(BOOL)isPublic
{
    NSString *documentsDirectory = isPublic ? [self getPublicPath] : [self getPrivatePath];
    NSArray<NSString *> *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
    NSMutableArray *fileList = [[NSMutableArray alloc] initWithCapacity:1];
    for (NSString *fileName in directoryContent) {
        if (prefix.length == 0 || [fileName hasPrefix:prefix]) {
            [fileList addObject:[documentsDirectory stringByAppendingPathComponent:fileName]];
        }
    }
    return fileList;
}
于 2020-06-19T08:42:12.833 回答