0

我有带有对象结构的测试数组 - 带有(NSMutableArray)项目的组,我将组保存在 YapDatabase

-(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] 只返回组中的一项

4

1 回答 1

1

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

看看 YapDatabase 的 wiki 文章“Storing Objects”,它讨论了它如何使用序列化器/解串器块:https ://github.com/yaptv/YapDatabase/wiki/Storing-Objects

基本上,当您分配/初始化 YapDatabase 实例时,您需要传递一个使用 Mantle 执行序列化/反序列化的序列化器和反序列化器块。

此外,请参阅 YapDatabase 可用的各种初始化方法:https ://github.com/yaptv/YapDatabase/blob/master/YapDatabase/YapDatabase.h

于 2015-09-28T05:14:40.773 回答