Commit 9a3a33e8 authored by Felix Paul Kühne's avatar Felix Paul Kühne

misc stability improvements if MLKit does not deliver a valid managed object...

misc stability improvements if MLKit does not deliver a valid managed object context or persistent store coordinator
parent e039313a
......@@ -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
......@@ -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];
......
......@@ -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;
......
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