YapDatabase sorting confounds editing update

72 Views Asked by At

In a UITableViewController I use YapDatabase with Mantle sorting the following way:

YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(NSString *group, NSString *collection1, NSString *key1, XQBuilding *object1, NSString *collection2, NSString *key2, XQBuilding *object2) {
    if ([group isEqualToString:XQBuildingsViewGroupName]) {
        return [[object1 name] compare:[object2 name] options:NSNumericSearch];
    }
    if ([group isEqualToString:XQPicturesGroup]) {
        return [[object1 updatedAt] compare:[object2 updatedAt]];
    }
    return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = NO;

YapDatabaseView *databaseView = [[YapDatabaseView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"" options:options];

Although the used option I have sometimes (when an edited name changes the edited item order in the list) incorrect indexPath on reading:

    - (UITableViewCell*)editableTableView:(UITableView *)tableView simpleCellForIndexPath:(NSIndexPath *)indexPath
    {
        __block XQBuilding *building;
        __block NSNumber *gla;
        [self.readConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
            building = [[transaction ext:XQBuildingListYapName] objectAtIndexPath:indexPath withMappings:self.tableViewAnimator.viewMappings];
            gla = [building glaWithTransaction:transaction];
        }];

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BuildingCell"];
        cell.textLabel.text = building.name;
        cell.detailTextLabel.text = [NSString stringWithFormat: @"%@ sqm", formatDecimal(gla)];
        return cell;
    }

i. e. building in such case is a different one than was edited. How to get a correct indexPath according to actual sorting?

1

There are 1 best solutions below

0
On

There are plenty of things that should be correctly coded to make it work properly!

As I can see from your code you use YapDatabaseViewMappings. Do you use LongLivedReadTransactions and do you subscribe to YapDatabaseModifiedNotification to properly modify the table on database changes?

There is an simplified example that shows how to use all these things to update your UITableView in real time right after database is updated.