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

Code before:

    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];
}

- (BOOL)isEqualToObject:(RLMObject *)object {
    // if identical object
    if (self == object) {
        return YES;
    }
    // if not in realm or differing realms
    if (_realm == nil || _realm != object.realm) {
        return NO;
    }
    // if either are detached
    if (!_row.is_attached() || !object->_row.is_attached()) {
        return NO;

Review comment (suggestion):
Also needs to validate that `prop` is a link or array property and that it actually links to this class.


Code after:

    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;
    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];
}

- (BOOL)isEqualToObject:(RLMObject *)object {
    // if identical object
    if (self == object) {
        return YES;
    }
    // if not in realm or differing realms