OSDN Git Service

りふぁくたりんぐ! JSONをCoreDataに入れる処理をメソッドにまとめた
authormasakih <masakih@users.sourceforge.jp>
Tue, 18 Feb 2014 13:52:34 +0000 (22:52 +0900)
committermasakih <masakih@users.sourceforge.jp>
Tue, 18 Feb 2014 13:52:34 +0000 (22:52 +0900)
KCD/HMJSONCommand.h
KCD/HMJSONCommand.m
KCD/HMMaserShipCommand.m
KCD/HMMasterFurnitureCommand.m
KCD/HMMasterMapAreaCommand.m
KCD/HMMasterMapCellCommand.m
KCD/HMMasterMapInfoCommand.m
KCD/HMMasterMissionCommand.m
KCD/HMMasterSTypeCommand.m
KCD/HMMasterSlotItemCommand.m
KCD/HMMasterUseItemCommand.m

index 9e3ad19..3a8b58d 100644 (file)
@@ -31,6 +31,8 @@ NSString *subAPI(NSString *api);
 + (BOOL)canExcuteAPI:(NSString *)api;
 
 
+- (void)commitJSONToEntityNamed:(NSString *)entityName;
+
 // default return NO
 - (BOOL)handleExtraValue:(id)value forKey:(NSString *)key toObject:(NSManagedObject *)object;
 
index c8615ca..6ed6e7b 100644 (file)
@@ -142,6 +142,67 @@ NSString *keyByDeletingPrefix(NSString *key)
        va_end(ap);
 }
 
+- (void)commitJSONToEntityNamed:(NSString *)entityName
+{
+       NSArray *api_data = [self.json objectForKey:@"api_data"];
+       if(![api_data isKindOfClass:[NSArray class]]) {
+               [self log:@"api_data is NOT NSArray."];
+               return;
+       }
+       
+       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
+       
+       for(NSDictionary *type in api_data) {
+               NSString *stypeID = type[@"api_id"];
+               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:entityName];
+               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
+               [req setPredicate:predicate];
+               NSError *error = nil;
+               id result = [managedObjectContext executeFetchRequest:req
+                                                                                                               error:&error];
+               if(error) {
+                       [self log:@"Fetch error: %@", error];
+                       continue;
+               }
+               NSManagedObject *object = nil;
+               if(!result || [result count] == 0) {
+                       object = [NSEntityDescription insertNewObjectForEntityForName:entityName
+                                                                                                  inManagedObjectContext:managedObjectContext];
+               } else {
+                       object = result[0];
+               }
+               
+               for(NSString *key in type) {
+                       id value = type[key];
+                       if([self handleExtraValue:value forKey:key toObject:object]) {
+                               continue;
+                       }
+                       if([value isKindOfClass:[NSArray class]]) {
+                               NSUInteger i = 0;
+                               for(id element in value) {
+                                       id hoge = element;
+                                       NSString *newKey = [NSString stringWithFormat:@"%@%ld", key, i];
+                                       if([object validateValue:&hoge forKey:newKey error:NULL]) {
+                                               [object setValue:hoge forKey:newKey];
+                                       }
+                                       i++;
+                               }
+                       } else if([value isKindOfClass:[NSDictionary class]]) {
+                               for(id subKey in value) {
+                                       id subValue = value[subKey];
+                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
+                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
+                                               [object setValue:subValue forKey:newKey];
+                                       }
+                               }
+                       } else {
+                               if([object validateValue:&value forKey:key error:NULL]) {
+                                       [object setValue:value forKey:key];
+                               }
+                       }
+               }
+       }
+}
 
 // abstruct
 - (void)execute
index 44ef293..84697fa 100644 (file)
@@ -64,59 +64,17 @@ static NSCondition *sCondition = nil;
        
        [object setValue:result[0] forKey:@"api_stype"];
 }
-- (void)realExecute
+- (BOOL)handleExtraValue:(id)value forKey:(NSString *)key toObject:(NSManagedObject *)object
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterShip"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterShip"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([key isEqualToString:@"api_stype"]) {
-                               [self setStype:value toObject:object];
-                               continue;
-                       }
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge =element;
-                                       if([object validateValue:&hoge forKey:key error:NULL]) {
-                                               [object setValue:hoge forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       }
-                                       i++;
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
+       if([key isEqualToString:@"api_stype"]) {
+               [self setStype:value toObject:object];
+               return YES;
        }
+       return NO;
+}
+- (void)realExecute
+{
+       [self commitJSONToEntityNamed:@"MasterShip"];
 }
-
 
 @end
index 03802e8..567b403 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterFurniture"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterFurniture"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge = element;
-                                       if([object validateValue:&hoge forKey:key error:NULL]) {
-                                               [object setValue:hoge forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       }
-                                       i++;
-                               }
-                       } else if([value isKindOfClass:[NSDictionary class]]) {
-                               for(id subKey in value) {
-                                       id subValue = value[subKey];
-                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
-                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
-                                               [object setValue:subValue forKey:newKey];
-                                       }
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterFurniture"];
 }
 @end
index 43a44e0..a70b16b 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterMapArea"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterMapArea"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([object validateValue:&value forKey:key error:NULL]) {
-                               [object setValue:value forKey:key];
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterMapArea"];
 }
 @end
index 402ed2c..e05965c 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterMapCell"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterMapCell"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([self handleExtraValue:value forKey:key toObject:object]) {
-                               continue;
-                       }
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge = element;
-                                       NSString *newKey = [NSString stringWithFormat:@"%@%ld", key, i];
-                                       if([object validateValue:&hoge forKey:newKey error:NULL]) {
-                                               [object setValue:hoge forKey:newKey];
-                                       }
-                                       i++;
-                               }
-                       } else if([value isKindOfClass:[NSDictionary class]]) {
-                               for(id subKey in value) {
-                                       id subValue = value[subKey];
-                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
-                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
-                                               [object setValue:subValue forKey:newKey];
-                                       }
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterMapCell"];
 }
 @end
index 17d7fe3..44a7eca 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterMapInfo"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterMapInfo"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge = element;
-                                       if([object validateValue:&hoge forKey:key error:NULL]) {
-                                               [object setValue:hoge forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       }
-                                       i++;
-                               }
-                       } else if([value isKindOfClass:[NSDictionary class]]) {
-                               for(id subKey in value) {
-                                       id subValue = value[subKey];
-                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
-                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
-                                               [object setValue:subValue forKey:newKey];
-                                       }
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterMapInfo"];
 }
 @end
index 4f68ffd..b6ed31c 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterMission"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterMission"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge = element;
-                                       if([object validateValue:&hoge forKey:key error:NULL]) {
-                                               [object setValue:hoge forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       }
-                                       i++;
-                               }
-                       } else if([value isKindOfClass:[NSDictionary class]]) {
-                               for(id subKey in value) {
-                                       id subValue = value[subKey];
-                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
-                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
-                                               [object setValue:subValue forKey:newKey];
-                                       }
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterMission"];
 }
 @end
index a60dc50..f26d768 100644 (file)
 
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
+       [self commitJSONToEntityNamed:@"MasterSType"];
        
        NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterSType"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                       error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterSType"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       [object setValue:type[key] forKey:key];
-               }
-       }
        [managedObjectContext save:NULL];
        
        NSCondition *lock = [HMMaserShipCommand condition];
index 3ab0657..8af7e49 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterSlotItem"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterSlotItem"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([value isKindOfClass:[NSArray class]]) {
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       id hoge = element;
-                                       if([object validateValue:&hoge forKey:key error:NULL]) {
-                                               [object setValue:hoge forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       }
-                                       i++;
-                               }
-                       } else if([value isKindOfClass:[NSDictionary class]]) {
-                               for(id subKey in value) {
-                                       id subValue = value[subKey];
-                                       NSString *newKey = [NSString stringWithFormat:@"%@_D_%@", key, keyByDeletingPrefix(subKey)];
-                                       if([object validateValue:&subValue forKey:newKey error:NULL]) {
-                                               [object setValue:subValue forKey:newKey];
-                                       }
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterSlotItem"];
 }
 @end
index 4196f30..8e111cf 100644 (file)
 }
 - (void)execute
 {
-       NSArray *api_data = [self.json objectForKey:@"api_data"];
-       if(![api_data isKindOfClass:[NSArray class]]) {
-               [self log:@"api_data is NOT NSArray."];
-               return;
-       }
-       
-       NSManagedObjectContext *managedObjectContext = [[NSApp delegate] managedObjectContext];
-       
-       for(NSDictionary *type in api_data) {
-               NSString *stypeID = type[@"api_id"];
-               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterUseItem"];
-               NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", stypeID];
-               [req setPredicate:predicate];
-               NSError *error = nil;
-               id result = [managedObjectContext executeFetchRequest:req
-                                                                                                               error:&error];
-               if(error) {
-                       [self log:@"Fetch error: %@", error];
-                       continue;
-               }
-               NSManagedObject *object = nil;
-               if(!result || [result count] == 0) {
-                       object = [NSEntityDescription insertNewObjectForEntityForName:@"MasterUseItem"
-                                                                                                  inManagedObjectContext:managedObjectContext];
-               } else {
-                       object = result[0];
-               }
-               
-               
-               for(NSString *key in type) {
-                       id value = type[key];
-                       if([value isKindOfClass:[NSArray class]]) {
-                               //
-                               NSUInteger i = 0;
-                               for(id element in value) {
-                                       [object setValue:value[i] forKey:[NSString stringWithFormat:@"%@%ld", key, i]];
-                                       i++;
-                               }
-                       } else {
-                               if([object validateValue:&value forKey:key error:NULL]) {
-                                       [object setValue:value forKey:key];
-                               }
-                       }
-               }
-       }
+       [self commitJSONToEntityNamed:@"MasterUseItem"];
 }
 @end