瀏覽代碼

WatchCommunication: add guards again crash when thumbnail is updated and watch is not pared or watch app not installed

Tobias Conradi 9 年之前
父節點
當前提交
b2d68ca3b2
共有 1 個文件被更改,包括 21 次插入2 次删除
  1. 21 2
      Sources/VLCWatchCommunication.m

+ 21 - 2
Sources/VLCWatchCommunication.m

@@ -39,8 +39,6 @@
             WCSession *session = [WCSession defaultSession];
             session.delegate = self;
             [session activateSession];
-            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(savedManagedObjectContextNotification:) name:NSManagedObjectContextDidSaveNotification object:nil];
-            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUpdateThumbnail:) name:MLFileThumbnailWasUpdated object:nil];
             _thumbnailingQueue = [NSOperationQueue new];
             _thumbnailingQueue.name = @"org.videolan.vlc.watch-thumbnailing";
         }
@@ -82,6 +80,7 @@ static VLCWatchCommunication *_singeltonInstance = nil;
     [vpc playMediaLibraryObject:managedObject];
 }
 
+#pragma mark - WCSessionDelegate
 - (NSDictionary *)handleMessage:(nonnull VLCWatchMessage *)message {
     UIApplication *application = [UIApplication sharedApplication];
     /* dispatch background task */
@@ -127,6 +126,19 @@ static VLCWatchCommunication *_singeltonInstance = nil;
     [self handleMessage:message];
 }
 
+- (void)sessionWatchStateDidChange:(WCSession *)session {
+
+    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+    [center removeObserver:self name:NSManagedObjectContextDidSaveNotification object:nil];
+    [center removeObserver:self name:MLFileThumbnailWasUpdated object:nil];
+
+    if ([[WCSession defaultSession] isPaired] && [[WCSession defaultSession] isWatchAppInstalled]) {
+        [center addObserver:self selector:@selector(savedManagedObjectContextNotification:) name:NSManagedObjectContextDidSaveNotification object:nil];
+        [center addObserver:self selector:@selector(didUpdateThumbnail:) name:MLFileThumbnailWasUpdated object:nil];
+    }
+}
+
+#pragma mark -
 
 - (void)setVolumeFromWatch:(VLCWatchMessage *)message
 {
@@ -232,6 +244,10 @@ static VLCWatchCommunication *_singeltonInstance = nil;
 
 - (void)transferThumbnailForObject:(NSManagedObject *__nonnull)object refreshCache:(BOOL)refresh{
 
+    if (![[WCSession defaultSession] isPaired] || ![[WCSession defaultSession] isWatchAppInstalled]) {
+        return;
+    }
+
     CGRect bounds = [WKInterfaceDevice currentDevice].screenBounds;
     CGFloat scale = [WKInterfaceDevice currentDevice].screenScale;
     [self.thumbnailingQueue addOperationWithBlock:^{
@@ -249,6 +265,9 @@ static VLCWatchCommunication *_singeltonInstance = nil;
 }
 
 - (void)transferImage:(UIImage *)image forObjectID:(NSManagedObjectID *)objectID {
+    if (!image || ![[WCSession defaultSession] isPaired] || ![[WCSession defaultSession] isWatchAppInstalled]) {
+        return;
+    }
 
     NSString *imageName = [[NSUUID UUID] UUIDString];
     NSURL *tmpDirectoryURL = [[WCSession defaultSession] watchDirectoryURL];