Commit 158f9d75 authored by Carola Nitz's avatar Carola Nitz

adjusted mediaLibrary to check for migrations

parent ba88c2cc
......@@ -29,6 +29,8 @@
@property (readonly) BOOL libraryNeedsUpgrade;
@property (nonatomic, strong) id delegate;
@property (nonatomic, strong) NSURL *persistentStoreURL;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+ (id)sharedMediaLibrary;
......
......@@ -173,23 +173,22 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return _documentFolderPath;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext)
return _managedObjectContext;
- (NSURL *)persistentStoreURL {
NSString *databaseFolderPath = [self databaseFolderPath];
NSString *path = [databaseFolderPath stringByAppendingPathComponent: @"MediaLibrary.sqlite"];
NSURL *url = [NSURL fileURLWithPath:path];
return [NSURL fileURLWithPath:path];
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSNumber *yes = @YES;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : yes,
NSInferMappingModelAutomaticallyOption : yes};
NSInferMappingModelAutomaticallyOption : yes};
NSError *error;
NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
if (!persistentStore) {
#if! TARGET_OS_IPHONE
......@@ -197,11 +196,11 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
NSInteger ret = NSRunAlertPanel(@"Error", @"The Media Library you have on your disk is not compatible with the one Lunettes can read. Do you want to create a new one?", @"No", @"Yes", nil);
if (ret == NSOKButton)
[NSApp terminate:nil];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
#else
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
#endif
persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
if (!persistentStore) {
#if! TARGET_OS_IPHONE
NSRunInformationalAlertPanel(@"Corrupted Media Library", @"There is nothing we can apparently do about it...", @"OK", nil, nil);
......@@ -213,9 +212,16 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return nil;
}
}
return coordinator;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext)
return _managedObjectContext;
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
[_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
[_managedObjectContext setUndoManager:nil];
[_managedObjectContext addObserver:self forKeyPath:@"hasChanges" options:NSKeyValueObservingOptionInitial context:nil];
return _managedObjectContext;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment