Explorar o código

fix db transfer to watch

Tobias Conradi %!s(int64=9) %!d(string=hai) anos
pai
achega
1a6c621980

+ 3 - 21
Sources/VLCWatchCommunication.m

@@ -183,29 +183,11 @@ static VLCWatchCommunication *_singeltonInstance = nil;
     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];
+    destOptions[NSSQLitePragmasOption] = @{@"journal_mode": @"DELETE"};
 
-    error = nil;
-    BOOL success = [migratePSC migratePersistentStore:migrateStore
-                                                toURL:tmpURL
-                                              options:destOptions
-                                             withType:NSSQLiteStoreType error:&error];
+    NSError *error;
+    bool success = [libraryPSC replacePersistentStoreAtURL:tmpURL destinationOptions:destOptions withPersistentStoreFromURL:persistentStore.URL sourceOptions:persistentStore.options storeType:NSSQLiteStoreType error:&error];
     if (!success) {
         NSLog(@"%s failed to copy persistent store to tmp location for copy to watch with error %@",__PRETTY_FUNCTION__,error);
     }

+ 13 - 13
VLC WatchKit Native Extension/Classes/ExtensionDelegate.swift

@@ -15,10 +15,16 @@ import WatchConnectivity
 import CoreData
 import MediaLibraryKit
 
-class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
+class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate, NSFileManagerDelegate {
 
     func applicationDidFinishLaunching() {
         // Perform any final initialization of your application.
+
+        let additionalOptions = [NSReadOnlyPersistentStoreOption : NSNumber(bool: true)]
+
+        let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
+        library.additionalPersitentStoreOptions = additionalOptions
+
         WCSession.defaultSession().delegate = self;
         WCSession.defaultSession().activateSession()
     }
@@ -40,22 +46,16 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
     func session(session: WCSession, didReceiveFile file: WCSessionFile) {
         let fileType = file.metadata?["filetype"] as? String ?? ""
         if fileType == "coredata" {
-            copyUpdatedCoreDataDBFromURL(file.fileURL)
+            dispatch_sync(dispatch_get_main_queue())
+            {
+                self.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")
-        }
+        let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
+        library.overrideLibraryWithLibraryFromURL(url)
         NSNotificationCenter.defaultCenter().postNotificationName(VLCDBUpdateNotification, object: self)
     }
 

+ 0 - 19
VLC WatchKit Native Extension/Classes/NSManagedObjectContext+refreshAll.h

@@ -1,19 +0,0 @@
-/*****************************************************************************
- * NSManagedObjectContext+refreshAll.h
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Tobias Conradi <videolan # tobias-conradi.de>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
-
-
-#import <CoreData/CoreData.h>
-
-@interface NSManagedObjectContext (refreshAll)
-// mergeChanges has same sematics as - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag;
-- (void)vlc_refreshAllObjectsMerge:(BOOL)mergeChanges;
-@end

+ 0 - 22
VLC WatchKit Native Extension/Classes/NSManagedObjectContext+refreshAll.m

@@ -1,22 +0,0 @@
-/*****************************************************************************
- * NSManagedObjectContext+refreshAll.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Tobias Conradi <videolan # tobias-conradi.de>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
-
-#import "NSManagedObjectContext+refreshAll.h"
-
-@implementation NSManagedObjectContext (refreshAll)
-- (void)vlc_refreshAllObjectsMerge:(BOOL)mergeChanges
-{
-    for (NSManagedObject *object in self.registeredObjects) {
-        [self refreshObject:object mergeChanges:mergeChanges];
-    }
-}
-@end

+ 21 - 13
VLC WatchKit Native Extension/Classes/VLCDetailInterfaceController.m

@@ -19,7 +19,8 @@
 #import <WatchConnectivity/WatchConnectivity.h>
 
 @interface VLCDetailInterfaceController ()
-@property (nonatomic, weak) NSManagedObject *managedObject;
+@property (nonatomic, weak) NSManagedObjectContext *moc;
+@property (nonatomic, strong) NSManagedObjectID *objectID;
 @end
 
 @implementation VLCDetailInterfaceController
@@ -32,18 +33,22 @@
     self.durationLabel.accessibilityLabel = NSLocalizedString(@"DURATION", nil);
 
     [self addNowPlayingMenu];
-    [self configureWithFile:context];
+
+    NSManagedObject *managedObject = context;
+    [self configureWithFile:managedObject];
+
+    self.objectID = managedObject.objectID;
+    self.moc = managedObject.managedObjectContext;
 }
 
 - (void)updateData {
     [super updateData];
-    NSManagedObject *managedObject = self.managedObject;
-    [managedObject.managedObjectContext refreshObject:managedObject mergeChanges:NO];
-    [self configureWithFile:managedObject];
+    [self.moc performBlock:^{
+        [self configureWithFile:[self.moc objectWithID:self.objectID]];
+    }];
 }
 
 - (void)configureWithFile:(NSManagedObject *)managedObject {
-    self.managedObject = managedObject;
 
     NSString *title = nil;
     NSString *durationString = nil;
@@ -63,11 +68,14 @@
     }
 
     BOOL playEnabled = managedObject != nil;
-    self.playNowButton.enabled = playEnabled;
 
-    self.mediaTitle = title;
-    self.mediaDuration = durationString;
-    self.playbackProgress = playbackProgress;
+    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+        self.playNowButton.enabled = playEnabled;
+
+        self.mediaTitle = title;
+        self.mediaDuration = durationString;
+        self.playbackProgress = playbackProgress;
+    }];
 
     /* do not block the main thread */
     [self performSelectorInBackground:@selector(loadThumbnailForManagedObject:) withObject:managedObject];
@@ -83,10 +91,10 @@
 
 - (IBAction)playNow {
 
-    id payload = self.managedObject.objectID.URIRepresentation.absoluteString;
+    NSURL *currentObjectURI = self.objectID.URIRepresentation;
     NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:@"playFile"
-                                                           payload:payload];
-    [self updateUserActivity:@"org.videolan.vlc-ios.playing" userInfo:@{@"playingmedia":self.managedObject.objectID.URIRepresentation} webpageURL:nil];
+                                                           payload:currentObjectURI.absoluteString];
+    [self updateUserActivity:@"org.videolan.vlc-ios.playing" userInfo:@{@"playingmedia":currentObjectURI} webpageURL:nil];
 
 
     [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {

+ 1 - 5
VLC WatchKit Native Extension/Classes/VLCPlaylistInterfaceController.m

@@ -16,7 +16,6 @@
 #import "VLCRowController.h"
 
 #import "VLCWatchTableController.h"
-#import "NSManagedObjectContext+refreshAll.h"
 #import "MLMediaLibrary+playlist.h"
 
 static NSString *const rowType = @"mediaRow";
@@ -38,9 +37,6 @@ static NSString *const rowType = @"mediaRow";
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
 
-    MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
-    mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
-
     if (context == nil) {
         self.libraryMode = VLCLibraryModeAllFiles;
         [self setupMenuButtons];
@@ -128,7 +124,7 @@ static NSString *const rowType = @"mediaRow";
 - (void)updateData {
     [super updateData];
     NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
-    [moc vlc_refreshAllObjectsMerge:NO];
+    [moc refreshAllObjects];
     self.tableController.objects = [self mediaArray];
 }
 

+ 0 - 6
VLC for iOS.xcodeproj/project.pbxproj

@@ -239,7 +239,6 @@
 		DD3567901B6761CE00338947 /* WatchConnectivity.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD35678F1B6761CD00338947 /* WatchConnectivity.framework */; };
 		DD3567B41B67674700338947 /* VLCTime.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3567B31B67674700338947 /* VLCTime.m */; };
 		DD3567EF1B6768FC00338947 /* ExtensionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD3567D91B6768FC00338947 /* ExtensionDelegate.swift */; };
-		DD3567F11B6768FC00338947 /* NSManagedObjectContext+refreshAll.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3567DC1B6768FC00338947 /* NSManagedObjectContext+refreshAll.m */; };
 		DD3567F21B6768FC00338947 /* VLCBaseInterfaceController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3567E01B6768FC00338947 /* VLCBaseInterfaceController.m */; };
 		DD3567F31B6768FC00338947 /* VLCDetailInterfaceController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3567E21B6768FC00338947 /* VLCDetailInterfaceController.m */; };
 		DD3567F51B6768FC00338947 /* VLCNowPlayingInterfaceController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD3567E61B6768FC00338947 /* VLCNowPlayingInterfaceController.m */; };
@@ -749,8 +748,6 @@
 		DD3567B31B67674700338947 /* VLCTime.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = VLCTime.m; path = ImportedSources/VLCKit/Sources/VLCTime.m; sourceTree = SOURCE_ROOT; };
 		DD3567B51B67675400338947 /* VLCTime.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = VLCTime.h; path = ImportedSources/VLCKit/Headers/Public/VLCTime.h; sourceTree = SOURCE_ROOT; };
 		DD3567D91B6768FC00338947 /* ExtensionDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionDelegate.swift; sourceTree = "<group>"; };
-		DD3567DB1B6768FC00338947 /* NSManagedObjectContext+refreshAll.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectContext+refreshAll.h"; sourceTree = "<group>"; };
-		DD3567DC1B6768FC00338947 /* NSManagedObjectContext+refreshAll.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectContext+refreshAll.m"; sourceTree = "<group>"; };
 		DD3567DD1B6768FC00338947 /* VLC for iOS WatchKit Extension-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "VLC for iOS WatchKit Extension-Prefix.pch"; sourceTree = "<group>"; };
 		DD3567DF1B6768FC00338947 /* VLCBaseInterfaceController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCBaseInterfaceController.h; sourceTree = "<group>"; };
 		DD3567E01B6768FC00338947 /* VLCBaseInterfaceController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCBaseInterfaceController.m; sourceTree = "<group>"; };
@@ -1703,8 +1700,6 @@
 			isa = PBXGroup;
 			children = (
 				DD3567D91B6768FC00338947 /* ExtensionDelegate.swift */,
-				DD3567DB1B6768FC00338947 /* NSManagedObjectContext+refreshAll.h */,
-				DD3567DC1B6768FC00338947 /* NSManagedObjectContext+refreshAll.m */,
 				DD3567DF1B6768FC00338947 /* VLCBaseInterfaceController.h */,
 				DD3567E01B6768FC00338947 /* VLCBaseInterfaceController.m */,
 				DD3567E11B6768FC00338947 /* VLCDetailInterfaceController.h */,
@@ -2203,7 +2198,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				DD3567F91B6768FC00338947 /* WKInterfaceObject+VLCProgress.m in Sources */,
-				DD3567F11B6768FC00338947 /* NSManagedObjectContext+refreshAll.m in Sources */,
 				DD3567EF1B6768FC00338947 /* ExtensionDelegate.swift in Sources */,
 				DD3567F81B6768FC00338947 /* VLCWatchTableController.m in Sources */,
 				DD3567F61B6768FC00338947 /* VLCPlaylistInterfaceController.m in Sources */,