Search code examples
iosobjective-cyapdatabase

Does YapDatabase have an equivalent to NSFetchedResultsController indexPathForObject:?


I have a UITableView that uses a YapDatabaseView and a YapDatabaseViewMappings. I am trying to programmatically have my table view scroll to the position of a given object in the database...

With CoreData I was doing:

NSIndexPath *indexPath = [self.fetchedResultsController indexPathForObject:myObject];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

Any equivalent to that scenario with YapDatabase?


Solution

  • Yes it does. You can use the API indexPathForKey(:inCollection:withMappings) which is available on YapDatabaseViewTransaction.

    e.g.

    YapDatabaseViewTransaction *viewTransaction = [transaction ext:"view name"];
    NSIndexPath *indexPath = [viewTransaction indexPathForKey(myObject.key, inCollection: myObject.collection, withMappings: mappings];
    

    This assumes that you can access the key and collection from your object.

    If you're using Swift, then check out TaylorSource which makes this a doddle.