source: branches/CoreData/SapphireFrappliance/MetaData/SapphireMObjects/SapphireDirector.m @ 840

Revision 840, 2.5 KB checked in by gbooker, 5 years ago (diff)

Delete unused objects as they become unused so as to not need the prune call later. This will help accelerate things later. Also, handled XML a bit better.

Line 
1#import "SapphireDirector.h"
2#import "CoreDataSupportFunctions.h"
3#import "SapphireFileSorter.h"
4#import "SapphireMetaDataSupport.h"
5
6@implementation SapphireDirector
7
8static NSArray *allowedSorts = nil;
9
10+ (void)load
11{
12        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireMovieTitleSorter sharedInstance], [SapphireDateSorter sharedInstance], [SapphireMovieIMDBRatingSorter sharedInstance], nil];
13}
14
15+ (SapphireDirector *)director:(NSString *)director inContext:(NSManagedObjectContext *)moc
16{
17        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", director];
18        return (SapphireDirector *)doSingleFetchRequest(SapphireDirectorName, moc, predicate);
19}
20
21+ (SapphireDirector *)createDirector:(NSString *)director inContext:(NSManagedObjectContext *)moc
22{
23        SapphireDirector *ret = [SapphireDirector director:director inContext:moc];
24        if(ret != nil)
25                return ret;
26       
27        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireDirectorName inManagedObjectContext:moc];
28        ret.name = director;
29        return ret;
30}
31
32+ (NSDictionary *)upgradeV1DirectorsFromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
33{
34        NSMutableDictionary *lookup = [NSMutableDictionary dictionary];
35        NSArray *oldDirectors = doFetchRequest(SapphireDirectorName, oldMoc, nil);
36        NSEnumerator *directorEnum = [oldDirectors objectEnumerator];
37        NSManagedObject *oldDirector;
38        while((oldDirector = [directorEnum nextObject]) != nil)
39        {
40                SapphireDirector *newDirector = [NSEntityDescription insertNewObjectForEntityForName:SapphireDirectorName inManagedObjectContext:newMoc];
41                NSString *name = [oldDirector valueForKey:@"name"];
42                newDirector.name = name;
43                newDirector.sortMethod = [oldDirector valueForKey:@"sortMethod"];
44                [lookup setObject:newDirector forKey:name];
45        }
46        return lookup;
47}
48
49- (NSPredicate *)metaFileFetchPredicate
50{
51        NSArray *movieIds = [self.moviesSet valueForKey:@"objectID"];
52        return [NSPredicate predicateWithFormat:@"movie IN %@", movieIds];
53}
54
55- (NSArray *)fileSorters
56{
57        return allowedSorts;
58}
59
60- (NSString *)path
61{
62        return [[super path] stringByAppendingPathComponent:self.name];
63}
64
65- (BOOL)shouldDelete
66{
67        return [self.moviesSet count] == 0;
68}
69
70-(void)removeMovies:(NSSet*)removedMovies
71{
72        [super removeMovies:removedMovies];
73        if([self.moviesSet count] == 0)
74                [SapphireMetaDataSupport setObjectForPendingDelete:self];
75}
76
77- (void)removeMoviesObject:(SapphireMovie *)removedMovie
78{
79        [super removeMoviesObject:removedMovie];
80        if([self.moviesSet count] == 0)
81                [SapphireMetaDataSupport setObjectForPendingDelete:self];
82}
83
84@end
Note: See TracBrowser for help on using the repository browser.