Commit e2643aba authored by Tobias's avatar Tobias

WIP copy full db to watch

very hacky and does not work correctly for updates
parent f9e5d4f5
......@@ -30,6 +30,7 @@
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(savedManagedObjectContextNotification:) name:NSManagedObjectContextDidSaveNotification object:nil];
}
}
return self;
......@@ -165,4 +166,52 @@ static VLCWatchCommunication *_singeltonInstance = nil;
}
}
#pragma mark - Copy CoreData to Watch
- (void)savedManagedObjectContextNotification:(NSNotification *)notification {
NSManagedObjectContext *moc = notification.object;
if (moc.persistentStoreCoordinator == [[MLMediaLibrary sharedMediaLibrary] persistentStoreCoordinator]) {
[self copyCoreDataToWatch];
}
}
- (void)copyCoreDataToWatch {
if (![[WCSession defaultSession] isReachable]) return;
MLMediaLibrary *library = [MLMediaLibrary sharedMediaLibrary];
NSPersistentStoreCoordinator *libraryPSC = [library persistentStoreCoordinator];
NSPersistentStore *persistentStore = [libraryPSC persistentStoreForURL:[library persistentStoreURL]];
NSURL *tmpURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:persistentStore.URL.lastPathComponent]];
NSPersistentStoreCoordinator *migratePSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:libraryPSC.managedObjectModel];
NSError *error;
NSPersistentStore *migrateStore = [migratePSC addPersistentStoreWithType:persistentStore.type
configuration:persistentStore.configurationName
URL:persistentStore.URL
options:persistentStore.options
error:&error];
if (!migrateStore) {
NSLog(@"%s failed to add persistent store with error %@",__PRETTY_FUNCTION__,error);
return;
}
NSMutableDictionary *destOptions = [persistentStore.options mutableCopy] ?: [NSMutableDictionary new];
destOptions[NSSQLitePragmasOption] = @{@"journal_mode": @"OFF"};
[migratePSC destroyPersistentStoreAtURL:tmpURL withType:persistentStore.type options:destOptions error:nil];
error = nil;
BOOL success = [migratePSC migratePersistentStore:migrateStore
toURL:tmpURL
options:destOptions
withType:NSSQLiteStoreType error:&error];
if (!success) {
NSLog(@"%s failed to copy persistent store to tmp location for copy to watch with error %@",__PRETTY_FUNCTION__,error);
}
NSDictionary *metadata = @{@"filetype":@"coredata"};
[[WCSession defaultSession] transferFile:tmpURL metadata:metadata];
}
@end
......@@ -13,6 +13,7 @@
import WatchKit
import WatchConnectivity
import CoreData
import MediaLibraryKit
class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
......@@ -20,7 +21,6 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
// Perform any final initialization of your application.
WCSession.defaultSession().delegate = self;
WCSession.defaultSession().activateSession()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
......@@ -38,6 +38,25 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
func session(session: WCSession, didReceiveFile file: WCSessionFile) {
// TODO: copy db and send update notification
let fileType = file.metadata?["filetype"] as? String ?? ""
if fileType == "coredata" {
copyUpdatedCoreDataDBFromURL(file.fileURL)
}
}
func copyUpdatedCoreDataDBFromURL(url:NSURL) {
let library = MLMediaLibrary.sharedMediaLibrary()
do {
// we can be sure that it's only the sqlite file and no -wal -shm etc. therefore we can just plain copy it.
if NSFileManager.defaultManager().fileExistsAtPath(library.persistentStoreURL!.absoluteString) {
try NSFileManager.defaultManager().replaceItemAtURL(library.persistentStoreURL, withItemAtURL: url, backupItemName: nil, options: NSFileManagerItemReplacementOptions.UsingNewMetadataOnly, resultingItemURL: nil)
} else {
try NSFileManager.defaultManager().copyItemAtURL(url, toURL: library.persistentStoreURL)
}
} catch {
print("failed to copy Core Data DB to new DB location on watch")
}
NSNotificationCenter.defaultCenter().postNotificationName(VLCDBUpdateNotification, object: self)
}
}
......@@ -3,4 +3,5 @@
//
#import "VLCWatchMessage.h"
\ No newline at end of file
#import "VLCWatchMessage.h"
#import "VLCBaseInterfaceController.h"
\ No newline at end of file
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