How to mapping array in YAPdatabase object?

438 Views Asked by At

i have test array with objects structure - Group with (NSMutableArray)items, and i save group in 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];
}];

The problem is that the group be NSMutabblArray and I want to see the objects in the table of the array, but [self.mappingsGroup numberOfItemsInSection:section] return only one items in group

1

There are 1 best solutions below

1
Hitesh Patel On

You need to configure YapDatabase to use Mantle. By default, it will use NSCoding. (Which is why you're seeing an error about "encodeWithCoder:", as that method is part of NSCoding.)

Take a look at YapDatabase's wiki article entitled "Storing Objects", which talks about how it uses the serializer/deserializer blocks: https://github.com/yaptv/YapDatabase/wiki/Storing-Objects

Basically, when you alloc/init your YapDatabase instance, you'll want to pass a serializer & deserializer block that uses Mantle to perform the serialization/deserialization.

Also, see the various init methods that are available for YapDatabase: https://github.com/yaptv/YapDatabase/blob/master/YapDatabase/YapDatabase.h