答案是,是的,我可以轻松地将 MySql 中的递归层次结构转换为 Core Data。这是简单的两遍算法。首先将所有父母插入数据库中。然后将每个孩子添加到其父母。
+ (void)populateCategories {
[[ROAPIManager sharedInstance] request:GET
endpoint:@"test_categories"
parameters:nil
success:^(NSArray *data) {
if ([data count] > 0) {
[self insertParentCategoriesIntoCoreDataFromArray:data];
[self insertChildCategoriesIntoCoreDataFromArray:data];
}
}
error:^(ROAPIError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:error.message delegate:nil cancelButtonTitle:NSLocalizedString(@"LocalizedOK", nil) otherButtonTitles:nil];
[alert show];
}
failed:^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"ErrorDuringSetup", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"LocalizedOK", nil) otherButtonTitles: nil];
[alert show];
}];
}
+ (void)insertParentCategoriesIntoCoreDataFromArray:(NSArray *)array {
ROAppDelegate *delegate = (ROAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSEntityDescription *categoryEntity = [NSEntityDescription entityForName:@"ROCategory" inManagedObjectContext:managedObjectContext];
for (int i = 0; i < [array count]; i++) {
ROCategory *category = [[ROCategory alloc] initWithEntity:categoryEntity insertIntoManagedObjectContext:managedObjectContext];
NSArray *mySQLRow = [array objectAtIndex:i];
NSString *name = [mySQLRow objectAtIndex:1];
[category setValue:name forKey:@"name"];
}
}
+ (void)insertChildCategoriesIntoCoreDataFromArray:(NSArray *)array {
ROAppDelegate *delegate = (ROAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSEntityDescription *categoryEntity = [NSEntityDescription entityForName:@"ROCategory" inManagedObjectContext:managedObjectContext];
for (int i = 0; i < [array count]; i++) {
NSArray *mySQLRow = [array objectAtIndex:i];
// can't add the topmost category as a child
if (![[mySQLRow objectAtIndex:2] isEqualToNumber:[NSNumber numberWithInt:-1]]) {
NSString *name = [mySQLRow objectAtIndex:1];
// fetch the child by name
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:categoryEntity];
request.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name LIKE[c] \'%@\'", name]];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
// major alert
}
if ([mutableFetchResults count] > 0) {
ROCategory *childEntity = [mutableFetchResults objectAtIndex:0];
[childEntity setValue:name forKey:@"name"];
NSNumber *parentId = [mySQLRow objectAtIndex:2];
ROCategory *parentEntity = [self parentEntityForId:parentId inArray:array];
if (parentEntity) {
NSMutableOrderedSet *children = [parentEntity mutableOrderedSetValueForKey:@"children"];
[children addObject:childEntity];
}
} else {
NSLog(@" ERROR there were no results returned");
}
}
}
}