2 // HMMaserShipCommand.m
5 // Created by Hori,Masaki on 2014/02/16.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMMaserShipCommand.h"
11 @interface HMMaserShipCommand ()
12 @property (nonatomic, strong) NSArray *masterSTypes;
15 @implementation HMMaserShipCommand
18 return @"api_data.api_mst_ship";
22 [self commitJSONToEntityNamed:@"MasterShip"];
25 - (void)setStype:(id)value toObject:(NSManagedObject *)object
27 id currentValue = [object valueForKeyPath:@"stype.name"];
28 if(currentValue && ![currentValue isEqual:[NSNull null]]
29 && [currentValue isEqual:value]) return;
31 if(!self.masterSTypes) {
32 NSManagedObjectContext *managedObjectContext = [object managedObjectContext];
33 NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterSType"];
34 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
35 [req setSortDescriptors:@[sortDescriptor]];
37 self.masterSTypes = [managedObjectContext executeFetchRequest:req
40 [self log:@"Fetch error: %@", error];
43 if(!self.masterSTypes || [self.masterSTypes count] == 0) {
44 [self log:@"Master SType is invalidate"];
49 NSRange range = NSMakeRange(0, self.masterSTypes.count);
50 NSUInteger index = [self.masterSTypes indexOfObject:value
52 options:NSBinarySearchingFirstEqual
53 usingComparator:^(id obj1, id obj2) {
55 if([obj1 isKindOfClass:[NSNumber class]]) {
58 value1 = [obj1 valueForKey:@"id"];
60 if([obj2 isKindOfClass:[NSNumber class]]) {
63 value2 = [obj2 valueForKey:@"id"];
65 return [value1 compare:value2];
67 if(index == NSNotFound) {
68 [self log:@"Could not find stype of id (%@)", value];
71 id item = [self.masterSTypes objectAtIndex:index];
73 [self setValueIfNeeded:item toObject:object forKey:@"stype"];
75 - (BOOL)handleExtraValue:(id)value forKey:(NSString *)key toObject:(NSManagedObject *)object
77 if([key isEqualToString:@"api_stype"]) {
78 [self setStype:value toObject:object];