2015-09-28 65 views
0

我有测试阵列与对象结构 - 集团(NSMutableArray的)物品,以及i保存组YapDatabase如何在YAP数据库对象中映射数组?

-(void)parseAndSaveJson:(id) json withCompleteBlock:(void(^)())completeBlock{ 

NSMutableArray *groupsArray = (NSMutableArray *)json; 

if (groupsArray != nil) { 

    YapDatabaseConnection *connectnion = [[DatabaseManager sharedYapDatabase] newConnection]; 

    [connectnion asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { 

     for (int groupIndex = 0; groupIndex < [groupsArray count]; groupIndex ++) { 

      LocalGroupsExercise *localGroup = [[LocalGroupsExercise alloc] init]; 

      localGroup.name = groupsArray[groupIndex][LOCAL_GROUPS_NAME]; 

      localGroup.tagColor = groupsArray[groupIndex][LOCAL_GROUPS_TAG_COLOR]; 

      localGroup.idGroup = [groupsArray[groupIndex][LOCAL_GROUPS_ID_GROUP] intValue]; 

      if (groupsArray[groupIndex][LOCAL_GROUPS_EXERCISES] != nil) { 

       NSMutableArray *exerciseArray = (NSMutableArray *)groupsArray[groupIndex][LOCAL_GROUPS_EXERCISES]; 

       for (int exerciseIndex = 0; exerciseIndex < [exerciseArray count]; exerciseIndex ++) { 

        LocalExercise *localExercise = [[LocalExercise alloc] init]; 

        localExercise.name = exerciseArray[exerciseIndex][EXERCISE_NAME]; 

        localExercise.exerciseId = [exerciseArray[exerciseIndex][LOCAL_EXERCISE_ID_EXERCISE] intValue]; 

        localExercise.groupId = localGroup.idGroup; 

        localExercise.type = [exerciseArray[exerciseIndex][EXERCISE_TYPE] intValue]; 

        localExercise.minWeight = [exerciseArray[exerciseIndex][EXERCISE_MIN_WEIGHT] floatValue]; 

        localExercise.maxWeight = [exerciseArray[exerciseIndex][EXERCISE_MAX_WEIGHT] floatValue]; 

        localExercise.minReps = [exerciseArray[exerciseIndex][EXERCISE_MIN_REPS] intValue]; 

        localExercise.maxReps = [exerciseArray[exerciseIndex][EXERCISE_MAX_REPS] intValue]; 

        localExercise.minTimer = [exerciseArray[exerciseIndex][EXERCISE_MIN_TIMER] intValue]; 

        localExercise.maxTimer = [exerciseArray[exerciseIndex][EXERCISE_MAX_TIMER] intValue]; 

        localExercise.timeRelax = [exerciseArray[exerciseIndex][EXERCISE_RELAX_TIME] intValue]; 

        [localGroup.exercises addObject:localExercise]; 

       } 
      } 

      [transaction setObject:localGroup forKey:[NSString stringWithFormat:@"%d", localGroup.idGroup] inCollection:LOCAL_GROUPS_CLASS_NAME]; 
     } 

     YapDatabaseConnection *connectnion = [[DatabaseManager sharedYapDatabase] newConnection]; 

     [connectnion readWithBlock:^(YapDatabaseReadTransaction *transaction) { 

      LocalGroupsExercise *group = [transaction objectForKey:@"2" inCollection:LOCAL_GROUPS_CLASS_NAME]; 

      NSLog(@"%@", group.name); 
      NSLog(@"%@", group.tagColor); 
      NSLog(@"%@", group.exercises); 

     }]; 

    } completionBlock:^{ 

     completeBlock(); 

    }]; 
} 

}

+ (YapDatabaseView *)setupDatabaseViewForShowGroupsGyms{ 

YapDatabaseViewGrouping *grouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) { 

    if ([object isKindOfClass:[LocalGroupsExercise class]]) { 

     LocalGroupsExercise *groupExercise = (LocalGroupsExercise *)object; 

     return [NSString stringWithFormat:@"%@", groupExercise.name]; 
    } 

    return nil; 
}]; 


YapDatabaseViewSorting *sorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, LocalGroupsExercise *obj1, NSString *collection2, NSString *key2, LocalGroupsExercise *obj2) { 

    return [obj1.name compare:obj2.name options:NSNumericSearch range:NSMakeRange(0, obj1.name.length)]; 
}]; 

YapDatabaseView *databaseView = [[YapDatabaseView alloc] initWithGrouping:grouping sorting:sorting versionTag:@"0"]; 

return databaseView; 

}

[[DatabaseManager sharedYapDatabase] registerExtension:self.databaseGroupView withName:LOCAL_GROUPS_CLASS_NAME]; 

[_connection beginLongLivedReadTransaction]; 


self.mappingsGroup = [[YapDatabaseViewMappings alloc] initWithGroupFilterBlock:^BOOL(NSString *group, YapDatabaseReadTransaction *transaction) { 

    return true; 

} sortBlock:^NSComparisonResult(NSString *group1, NSString *group2, YapDatabaseReadTransaction *transaction) { 

    return [group1 compare:group2]; 

} view:LOCAL_GROUPS_CLASS_NAME]; 


[_connection readWithBlock:^(YapDatabaseReadTransaction *transaction) { 
    [self.mappingsGroup updateWithTransaction:transaction]; 
}]; 

的问题是该组为NSMutabblArray,我想查看数组表中的对象,但[self.mappingsGroup numberOfItemsInSection:section]只返回组中的一个项目

回答

1

您需要配置YapDatabase才能使用Mantle。默认情况下,它将使用NSCoding。 (这就是为什么你看到有关“encodeWithCoder:”的错误,因为该方法是NSCoding的一部分。)

看看YapDatabase的wiki文章标题为“存储对象”,它讨论了它如何使用串行器/解串器模块:https://github.com/yaptv/YapDatabase/wiki/Storing-Objects

基本上,当你分配/初始化你YapDatabase例如,你会希望通过使用地幔进行序列化/反序列化串行解串器&块。

而且,看到各种初始化方法可供YapDatabase:https://github.com/yaptv/YapDatabase/blob/master/YapDatabase/YapDatabase.h

+0

我使用NSCoding,并没有看到错误,问题是,该集团是NSMutabblArray,我希望看到的表对象排列 –