
我有两个对象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对节进行排序?
发布于 2015-09-30 10:52:50
我用代码解决了问题
_fetchedResultsController = [Exercise MR_fetchAllGroupedBy:@"groupEdge" withPredicate:nil sortedBy:@"uidGroup,uid" ascending:true];
https://stackoverflow.com/questions/32834932
复制相似问题