MagicalRecord извлекает две сущности со строкой и разделами и сортирует разделы
У меня есть два объекта GroupExercise <---- >> Упражнение, и нужно сделать запрос выборки для tableView с разделами (заголовок GroupExercise.name) и строк (Exercise.name), как я могу получить две сущности с помощью магической записи или основных данных
Упражнение объект
@interface Exercise : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * timeRelax;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSNumber * uidGroup;
@property (nonatomic, retain) GroupExercise *groupEdge;
GroupExerciseObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * tagColor;
@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSOrderedSet *exerciseEdge;
сохранить объекты с кодом
- (void)parseAndSaveJson:(id)json withCompleteBlock:(void (^)())completeBlock {
NSMutableArray *groupsArray = (NSMutableArray *) json;
NSLog(@"%@", json);
if (groupsArray != nil) {
NSArray *allEntities = [NSManagedObjectModel MR_defaultManagedObjectModel].entities;
[allEntities enumerateObjectsUsingBlock:^(NSEntityDescription *entityDescription, NSUInteger idx, BOOL *stop) {
[NSClassFromString([entityDescription managedObjectClassName]) MR_truncateAll];
}];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (int groupIndex = 0; groupIndex < [groupsArray count]; groupIndex++) {
GroupExercise *localGroup = [GroupExercise MR_createEntityInContext:localContext];
localGroup.name = groupsArray[groupIndex][LOCAL_GROUPS_NAME];
localGroup.tagColor = groupsArray[groupIndex][LOCAL_GROUPS_TAG_COLOR];
localGroup.uid = @([groupsArray[groupIndex][LOCAL_GROUPS_ID_GROUP] intValue]);
NSMutableArray *exerciseArray = (NSMutableArray *) groupsArray[groupIndex][LOCAL_GROUPS_EXERCISES];
NSMutableSet *set = [[NSMutableSet alloc] init];
for (int exerciseIndex = 0; exerciseIndex < [exerciseArray count]; exerciseIndex++) {
Exercise *exercise = [Exercise MR_createEntityInContext:localContext];
exercise.name = exerciseArray[exerciseIndex][EXERCISE_NAME];
exercise.uid = @([exerciseArray[exerciseIndex][LOCAL_EXERCISE_ID_EXERCISE] intValue]);
exercise.uidGroup = @([groupsArray[groupIndex][LOCAL_GROUPS_ID_GROUP] intValue]);
exercise.groupEdge = localGroup;
[set addObject:exercise];
}
[localGroup addExerciseEdge:set];
}
} completion:^(BOOL contextDidSave, NSError *error) {
completeBlock();
}];
}
}
и использовать FRC
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
_fetchedResultsController = [Exercise MR_fetchAllGroupedBy:@"groupEdge" withPredicate:nil sortedBy:@"uid" ascending:true];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
Как я могу сортировать разделы с groupEdge.name?
1 ответ
Я решил с кодом
_fetchedResultsController = [Exercise MR_fetchAllGroupedBy:@"groupEdge" withPredicate:nil sortedBy:@"uidGroup,uid" ascending:true];