我在我的应用程序中使用核心数据,我想在下次更新时启动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文件“
答案 0 :(得分: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
是空的。我不知道为什么,因为你没有发布代码给你一个值,但这就是造成这次崩溃的原因。
答案 1 :(得分:0)
答案结果很简单,就像通常那样!
我刚刚移动了将应用程序原始数据库复制到的所需代码 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator在添加商店之前而不是尝试从 - (BOOL)应用程序:(UIApplication *)应用程序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;
}
答案 2 :(得分: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;
}