Repository: realm/realm-swift
File: src/realm/objc/RLMTable.mm (Objective-C++)

Code before:
{
    if (!([self columnTypeOfColumnWithIndex:colIndex] == RLMTypeString)) {
        @throw [NSException exceptionWithName:@"realm:column_type_not_supported"
                                       reason:@"Distinct currently only supported on columns of type RLMTypeString"
                                     userInfo:nil];
    }
    if (![self isIndexCreatedInColumnWithIndex:colIndex]) {
        @throw [NSException exceptionWithName:@"realm:column_not_indexed"
                                       reason:@"An index must be created on the column to get distinct values"
                                     userInfo:nil];
    }
    
    tightdb::TableView distinctView = m_table->get_distinct_view(colIndex);
    return [RLMView viewWithTable:self nativeView:distinctView];
}

-(id)firstWhere:(id)predicate, ...
{
    NSPredicate *finalPredicate = nil;
    if ([predicate isKindOfClass:[NSPredicate class]]) {
        finalPredicate = predicate;
    } else if ([predicate isKindOfClass:[NSString class]]) {
        va_list args;
        va_start(args, predicate);
        finalPredicate = [NSPredicate predicateWithFormat:predicate arguments:args];
        va_end(args);
    } else if (predicate) {
        @throw predicate_exception(@"Invalid value",
                                   @"firstWhere: can only accept an NSPredicate or an NSString with optional format va_list");
    }
    
    tightdb::Query query = queryFromPredicate(self, finalPredicate);
    
    size_t row_ndx = query.find();
    
    if (row_ndx == tightdb::not_found)
        return nil;
    
    return [[_proxyObjectClass alloc] initWithTable:self ndx:row_ndx];
}

-(RLMView *)allWhere:(id)predicate, ...
{
    NSPredicate *finalPredicate = nil;
    if ([predicate isKindOfClass:[NSPredicate class]]) {
        finalPredicate = predicate;
    } else if ([predicate isKindOfClass:[NSString class]]) {
        va_list args;
        va_start(args, predicate);
        finalPredicate = [NSPredicate predicateWithFormat:predicate arguments:args];
        va_end(args);

Review comment (refactor):
It appears there this added part is present multiple places. Can't it be refactored to a common function?


Code after:

-(RLMQuery*)whereWithError:(NSError* __autoreleasing*)error
{
    return [[RLMQuery alloc] initWithTable:self error:error];
}

-(RLMView *)distinctValuesInColumnWithIndex:(NSUInteger)colIndex
{
    if (!([self columnTypeOfColumnWithIndex:colIndex] == RLMTypeString)) {
        @throw [NSException exceptionWithName:@"realm:column_type_not_supported"
                                       reason:@"Distinct currently only supported on columns of type RLMTypeString"
                                     userInfo:nil];
    }
    if (![self isIndexCreatedInColumnWithIndex:colIndex]) {
        @throw [NSException exceptionWithName:@"realm:column_not_indexed"
                                       reason:@"An index must be created on the column to get distinct values"
                                     userInfo:nil];
    }
    
    tightdb::TableView distinctView = m_table->get_distinct_view(colIndex);
    return [RLMView viewWithTable:self nativeView:distinctView];
}

-(id)firstWhere:(id)predicate, ...
{
    NSPredicate *finalPredicate = nil;
    RLM_PREDICATE(predicate, finalPredicate);
    
    tightdb::Query query = queryFromPredicate(self, finalPredicate);
    
    size_t row_ndx = query.find();
    
    if (row_ndx == tightdb::not_found)
        return nil;
    
    return [[_proxyObjectClass alloc] initWithTable:self ndx:row_ndx];
}

-(RLMView *)allWhere:(id)predicate, ...
{
    NSPredicate *finalPredicate = nil;
    RLM_PREDICATE(predicate, finalPredicate);
    
    tightdb::Query query = queryFromPredicate(self, finalPredicate);

    // create view
    tightdb::TableView view = query.find_all();
    
    // create objc view and return
    return [RLMView viewWithTable:self nativeView:view objectClass:_proxyObjectClass];
}