Repository: realm/realm-swift
File: Realm/RLMObject.mm (Objective-C++)

Code before:
    NSMutableString *mString = [NSMutableString stringWithFormat:@"%@ {\n", baseClassName];

    for (RLMProperty *property in objectSchema.properties) {
        id object = self[property.name];
        NSString *sub;
        if ([object respondsToSelector:@selector(descriptionWithMaxDepth:)]) {
            sub = [object descriptionWithMaxDepth:depth - 1];
        }
        else {
            sub = [object description];
        }
        [mString appendFormat:@"\t%@ = %@;\n", property.name, sub];
    }
    [mString appendString:@"}"];

    return [NSString stringWithString:mString];
}

- (BOOL)isDeletedFromRealm {
    // if not standalone and our accessor has been detached, we have been deleted
    return self.class == self.objectSchema.accessorClass && !_row.is_attached();
}

- (NSArray *)linkingObjectsOfClass:(NSString *)className forProperty:(NSString *)property {
    if (!_realm) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Linking object only available for objects in a Realm."
                                     userInfo:nil];
    }
    RLMCheckThread(_realm);

    if (!_row.is_attached()) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Object has been deleted and is no longer valid."
                                     userInfo:nil];
    }

    RLMObjectSchema *schema = _realm.schema[className];
    RLMProperty *prop = schema[property];
    if (!prop) {
        @throw [NSException exceptionWithName:@"RLMException" reason:[NSString stringWithFormat:@"Invalid property '%@'", property] userInfo:nil];
    }

    Table &table = *schema->_table.get();
    size_t col = prop.column;
    NSUInteger count = _row.get_backlink_count(table, col);
    NSMutableArray *links = [NSMutableArray arrayWithCapacity:count];
    for (NSUInteger i = 0; i < count; i++) {
        [links addObject:RLMCreateObjectAccessor(_realm, className, _row.get_backlink(table, col, i))];
    }
    return [links copy];

Review comment (suggestion):
I think it does need to be an error - if you have multiple standalone objects linking to each other you would expect to get standalone objects back, not an empty array. Either we need proper standalone support which isn't really feasible or we need to disallow calling this on standalone objects.


Code after:
    NSMutableString *mString = [NSMutableString stringWithFormat:@"%@ {\n", baseClassName];

    for (RLMProperty *property in objectSchema.properties) {
        id object = self[property.name];
        NSString *sub;
        if ([object respondsToSelector:@selector(descriptionWithMaxDepth:)]) {
            sub = [object descriptionWithMaxDepth:depth - 1];
        }
        else {
            sub = [object description];
        }
        [mString appendFormat:@"\t%@ = %@;\n", property.name, sub];
    }
    [mString appendString:@"}"];

    return [NSString stringWithString:mString];
}

- (BOOL)isDeletedFromRealm {
    // if not standalone and our accessor has been detached, we have been deleted
    return self.class == self.objectSchema.accessorClass && !_row.is_attached();
}

- (NSArray *)linkingObjectsOfClass:(NSString *)className forProperty:(NSString *)property {
    if (!_realm) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Linking object only available for objects in a Realm."
                                     userInfo:nil];
    }
    RLMCheckThread(_realm);

    if (!_row.is_attached()) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Object has been deleted and is no longer valid."
                                     userInfo:nil];
    }

    RLMObjectSchema *schema = _realm.schema[className];
    RLMProperty *prop = schema[property];
    if (!prop) {
        @throw [NSException exceptionWithName:@"RLMException" reason:[NSString stringWithFormat:@"Invalid property '%@'", property] userInfo:nil];
    }

    if (![prop.objectClassName isEqualToString:_objectSchema.className]) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:[NSString stringWithFormat:@"Property '%@' of '%@' expected to be an RLMObject or RLMArray property pointing to type '%@'", property, className, _objectSchema.className]
                                     userInfo:nil];
    }

    Table &table = *schema->_table.get();
    size_t col = prop.column;