OSDN Git Service

deleteObject:をHMCoreDataManagerで行うように変更
[kcd/KCD.git] / KCD / HMHistoryItemCleaner.m
1 //
2 //  HMHistoryItemCleaner.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/06/27.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMHistoryItemCleaner.h"
10
11 #import "HMUserDefaults.h"
12
13 #import "HMLocalDataStore.h"
14
15
16 @implementation HMHistoryItemCleaner
17
18 - (void)cleanOldHistoryItems
19 {
20         if(!HMStandardDefaults.cleanOldHistoryItems) return;
21         
22         HMLocalDataStore *store = [HMLocalDataStore oneTimeEditor];
23         
24         NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-1 * HMStandardDefaults.cleanSiceDays * 24 * 60 * 60];
25         NSPredicate *p01 = [NSPredicate predicateWithFormat:@"date < %@", date];
26         NSPredicate *p02 = [NSPredicate predicateWithFormat:@"mark = 0 || mark = nil"];
27         NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p01, p02]];
28         
29         NSError *error = nil;
30         NSArray<NSManagedObject *> *array = [store objectsWithEntityName:@"KaihatuHistory"
31                                                                                                                                   predicate:predicate
32                                                                                                                                           error:&error];
33         if(!error) {
34                 for(NSManagedObject *obj in array) {
35                         [store deleteObject:obj];
36                 }
37         } else {
38                 NSLog(@"%s ERROR: KaihatuHistory, %@", __PRETTY_FUNCTION__, error);
39         }
40         
41         error = nil;
42         array = [store objectsWithEntityName:@"KenzoHistory"
43                                                            predicate:predicate
44                                                                    error:&error];
45         if(!error) {
46                 for(NSManagedObject *obj in array) {
47                         [store deleteObject:obj];
48                 }
49         } else {
50                 NSLog(@"%s ERROR: KenzoHistory, %@", __PRETTY_FUNCTION__, error);
51         }
52         
53         NSArray *area = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"];
54         NSPredicate *p03 = [NSPredicate predicateWithFormat:@"mapArea IN %@", area];
55         predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, p03]];
56         
57         error = nil;
58         array = [store objectsWithEntityName:@"DropShipHistory"
59                                                            predicate:predicate
60                                                                    error:&error];
61         if(!error) {
62                 for(NSManagedObject *obj in array) {
63                         [store deleteObject:obj];
64                 }
65         } else {
66                 NSLog(@"%s ERROR: DropShipHistory, %@", __PRETTY_FUNCTION__, error);
67         }
68 }
69 @end