Seb*_*ger 6 iphone core-data objective-c ios
我有一个(可以)简单的修复我的coredata持久性商店的问题.
我创建它:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator != nil)
{
return persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dummyURL.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;
}
Run Code Online (Sandbox Code Playgroud)
使用网址 dummyURL.sqlite
我在使用该项目的第一天就这样做了,并且忘记了重命名..现在我所有现有的测试设备(4)都在使用超过2个月,使用该应用程序,收集大量数据并存储它一个愚蠢的网址^^
更新我做了一些关于持久性存储迁移的研究并编写了这个函数:
-(void)migrate{
NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator];
NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
NSError *error = nil;
NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL];
NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld
toURL:newURL
options:nil
withType:NSSQLiteStoreType
error:&error];
}
Run Code Online (Sandbox Code Playgroud)
问题1 will this work or will i lose some data with that?
问题2 afterwards will i just have to change the appendString in my persistenstore function?
我设法自己使用 migratePersistentStore 函数解决了这个问题:
-(void)migrate{
NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator];
NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
NSError *error = nil;
NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL];
NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld
toURL:newURL
options:nil
withType:NSSQLiteStoreType
error:&error];
}
Run Code Online (Sandbox Code Playgroud)
database.sqli之后我只是将appDelegate 中的appendURL 更改为。奇迹般有效 :)
| 归档时间: |
|
| 查看次数: |
1362 次 |
| 最近记录: |