Commit 66481080 authored by Tobias's avatar Tobias

add method to replace current library with other library

parent 03a0ca9e
...@@ -46,6 +46,8 @@ ...@@ -46,6 +46,8 @@
- (BOOL)libraryMigrationNeeded; - (BOOL)libraryMigrationNeeded;
- (void)migrateLibrary; - (void)migrateLibrary;
- (void)overrideLibraryWithLibraryFromURL:(NSURL *)replacementURL;
- (void)addFilePaths:(NSArray *)filepaths; - (void)addFilePaths:(NSArray *)filepaths;
- (void)updateMediaDatabase; - (void)updateMediaDatabase;
...@@ -70,6 +72,8 @@ ...@@ -70,6 +72,8 @@
- (void)save; - (void)save;
- (void)libraryDidDisappear; - (void)libraryDidDisappear;
- (void)libraryDidAppear; - (void)libraryDidAppear;
@end @end
@protocol MLMediaLibrary <NSObject> @protocol MLMediaLibrary <NSObject>
......
...@@ -247,22 +247,28 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -247,22 +247,28 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
} }
#pragma mark - #pragma mark -
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; - (NSPersistentStore *)addDefaultLibraryStoreToCoordinator:(NSPersistentStoreCoordinator *)coordinator withError:(NSError **)error {
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSSQLitePragmasOption : @{@"journal_mode": @"DELETE"}};
NSNumber *yes = @YES;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : yes,
NSInferMappingModelAutomaticallyOption : yes};
if (self.additionalPersitentStoreOptions.count > 0) { if (self.additionalPersitentStoreOptions.count > 0) {
NSMutableDictionary *mutableOptions = options.mutableCopy; NSMutableDictionary *mutableOptions = options.mutableCopy;
[mutableOptions addEntriesFromDictionary:self.additionalPersitentStoreOptions]; [mutableOptions addEntriesFromDictionary:self.additionalPersitentStoreOptions];
options = mutableOptions; options = mutableOptions;
} }
return [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:error];
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if ([[self.additionalPersitentStoreOptions objectForKey:NSReadOnlyPersistentStoreOption] boolValue] == YES) { if ([[self.additionalPersitentStoreOptions objectForKey:NSReadOnlyPersistentStoreOption] boolValue] == YES) {
if (![[NSFileManager defaultManager] fileExistsAtPath:self.persistentStoreURL.path]) { if (![[NSFileManager defaultManager] fileExistsAtPath:self.persistentStoreURL.path]) {
...@@ -272,7 +278,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -272,7 +278,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
} }
NSError *error; NSError *error;
NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error]; NSPersistentStore *persistentStore = [self addDefaultLibraryStoreToCoordinator:coordinator withError:&error];
if (!persistentStore) { if (!persistentStore) {
#if! TARGET_OS_IPHONE #if! TARGET_OS_IPHONE
...@@ -284,7 +290,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -284,7 +290,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
#else #else
[[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil]; [[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
#endif #endif
persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error]; persistentStore = [self addDefaultLibraryStoreToCoordinator:coordinator withError:&error];
if (!persistentStore) { if (!persistentStore) {
#if! TARGET_OS_IPHONE #if! TARGET_OS_IPHONE
NSRunInformationalAlertPanel(@"Corrupted Media Library", @"There is nothing we can apparently do about it...", @"OK", nil, nil); NSRunInformationalAlertPanel(@"Corrupted Media Library", @"There is nothing we can apparently do about it...", @"OK", nil, nil);
...@@ -303,13 +309,48 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -303,13 +309,48 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return coordinator; return coordinator;
} }
- (void)overrideLibraryWithLibraryFromURL:(NSURL *)replacementURL {
NSError *error;
NSPersistentStoreCoordinator *psc = self.persistentStoreCoordinator;
NSPersistentStore *store = [psc persistentStoreForURL:self.persistentStoreURL];
if (store) {
if(![psc removePersistentStore:store error:&error]) {
NSLog(@"%s failed to remove persistent store with error %@",__PRETTY_FUNCTION__,error);
error = nil;
}
}
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager replaceItemAtURL:self.persistentStoreURL
withItemAtURL:replacementURL
backupItemName:nil
options:0
resultingItemURL:nil
error:&error];
if (!success) {
NSLog(@"%s failed to replace store with error %@",__PRETTY_FUNCTION__,error);
error = nil;
}
if(![self addDefaultLibraryStoreToCoordinator:psc withError:&error]) {
NSLog(@"%s failed to add store with error %@",__PRETTY_FUNCTION__,error);
}
}
- (NSManagedObjectContext *)managedObjectContext - (NSManagedObjectContext *)managedObjectContext
{ {
if (_managedObjectContext) if (_managedObjectContext)
return _managedObjectContext; return _managedObjectContext;
NSPersistentStoreCoordinator *coodinator = self.persistentStoreCoordinator;
if (!coodinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; [_managedObjectContext setPersistentStoreCoordinator:coodinator];
if (_managedObjectContext.persistentStoreCoordinator == nil) { if (_managedObjectContext.persistentStoreCoordinator == nil) {
_managedObjectContext = nil; _managedObjectContext = nil;
return nil; return nil;
......
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