Przeglądaj źródła

misc stability improvements if MLKit does not deliver a valid managed object context or persistent store coordinator

Felix Paul Kühne 10 lat temu
rodzic
commit
9a3a33e836

+ 9 - 4
Sources/VLCAppDelegate.m

@@ -588,10 +588,15 @@
     MLMediaLibrary *library = [MLMediaLibrary sharedMediaLibrary];
     NSString *uriString = userInfo[@"URIRepresentation"];
     NSURL *uriRepresentation = [NSURL URLWithString:uriString];
-    NSManagedObjectID *objectID = [library.persistentStoreCoordinator managedObjectIDForURIRepresentation:uriRepresentation];
-    NSManagedObjectContext *moc = [(id)library managedObjectContext];
-    NSManagedObject *managedObject = [moc objectWithID:objectID];
-    [self openMediaFromManagedObject:managedObject];
+    NSPersistentStoreCoordinator *persistentStoreCoordinator = library.persistentStoreCoordinator;
+    if (persistentStoreCoordinator) {
+        NSManagedObjectID *objectID = [persistentStoreCoordinator managedObjectIDForURIRepresentation:uriRepresentation];
+        NSManagedObjectContext *moc = [(id)library managedObjectContext];
+        if (moc) {
+            NSManagedObject *managedObject = [moc objectWithID:objectID];
+            [self openMediaFromManagedObject:managedObject];
+        }
+    }
 }
 
 @end

+ 25 - 21
Sources/VLCHTTPConnection.m

@@ -109,27 +109,31 @@
         filePath = [filePath stringByReplacingOccurrencesOfString:@".png" withString:@""];
 
         NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
-        NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
-        NSManagedObject *mo = [moc existingObjectWithID:[psc managedObjectIDForURIRepresentation:[NSURL URLWithString:filePath]] error:nil];
-
-        NSData *theData;
-        if ([mo isKindOfClass:[MLFile class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:(MLFile *)mo]);
-        else if ([mo isKindOfClass:[MLShow class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForShow:(MLShow *)mo]);
-        else if ([mo isKindOfClass:[MLLabel class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForLabel:(MLLabel *)mo]);
-        else if ([mo isKindOfClass:[MLAlbum class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[[(MLAlbum *)mo tracks].anyObject files].anyObject]);
-        else if ([mo isKindOfClass:[MLAlbumTrack class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[(MLAlbumTrack *)mo files].anyObject]);
-        else if ([mo isKindOfClass:[MLShowEpisode class]])
-            theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[(MLShowEpisode *)mo files].anyObject]);
-
-        if (theData) {
-            HTTPDataResponse *dataResponse = [[HTTPDataResponse alloc] initWithData:theData];
-            dataResponse.contentType = @"image/png";
-            return dataResponse;
+        if (moc) {
+            NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
+            if (psc) {
+                NSManagedObject *mo = [moc existingObjectWithID:[psc managedObjectIDForURIRepresentation:[NSURL URLWithString:filePath]] error:nil];
+
+                NSData *theData;
+                if ([mo isKindOfClass:[MLFile class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:(MLFile *)mo]);
+                else if ([mo isKindOfClass:[MLShow class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForShow:(MLShow *)mo]);
+                else if ([mo isKindOfClass:[MLLabel class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForLabel:(MLLabel *)mo]);
+                else if ([mo isKindOfClass:[MLAlbum class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[[(MLAlbum *)mo tracks].anyObject files].anyObject]);
+                else if ([mo isKindOfClass:[MLAlbumTrack class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[(MLAlbumTrack *)mo files].anyObject]);
+                else if ([mo isKindOfClass:[MLShowEpisode class]])
+                    theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForMediaFile:[(MLShowEpisode *)mo files].anyObject]);
+
+                if (theData) {
+                    HTTPDataResponse *dataResponse = [[HTTPDataResponse alloc] initWithData:theData];
+                    dataResponse.contentType = @"image/png";
+                    return dataResponse;
+                }
+            }
         }
     }
     NSString *filePath = [self filePathForURI:path];

+ 10 - 8
Sources/VLCMovieViewController.m

@@ -760,16 +760,18 @@ typedef NS_ENUM(NSInteger, VLCPanType) {
 
         NSFetchRequest *request = [[NSFetchRequest alloc] init];
         NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
-        NSEntityDescription *entity = [NSEntityDescription entityForName:@"File" inManagedObjectContext:moc];
-        [request setEntity:entity];
-        [request setPredicate:[NSPredicate predicateWithFormat:@"url CONTAINS %@", componentString]];
+        if (moc) {
+            NSEntityDescription *entity = [NSEntityDescription entityForName:@"File" inManagedObjectContext:moc];
+            [request setEntity:entity];
+            [request setPredicate:[NSPredicate predicateWithFormat:@"url CONTAINS %@", componentString]];
 
-        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
-        [request setSortDescriptors:@[descriptor]];
+            NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
+            [request setSortDescriptors:@[descriptor]];
 
-        NSArray *matches = [moc executeFetchRequest:request error:nil];
-        if (matches.count > 0)
-            matchedFile = matches[0];
+            NSArray *matches = [moc executeFetchRequest:request error:nil];
+            if (matches.count > 0)
+                matchedFile = matches[0];
+        }
     }
     if (matchedFile.lastPosition)
         lastPosition = matchedFile.lastPosition.floatValue;