Skip to content

Commit

Permalink
Property initialize the schema for frozen Realms
Browse files Browse the repository at this point in the history
How we initialize the schema results in RealmCoordinator's schema cache not
containing computed properties, so we need to call set_schema_subset() on
frozen Realms to populate them.
  • Loading branch information
tgoyne committed Jun 10, 2020
1 parent d609e12 commit 94e356e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ x.y.z Release notes (yyyy-MM-dd)
* Freezing an object within the write transaction that the object was created
in now throws an exception rather than crashing when the object is first
used.
* The schema for frozen Realms was not properly initialized, leading to crashes
when accessing a RLMLinkingObjects property.
([#6568](https://github.com/realm/realm-cocoa/issues/6568), since 5.0.0).

<!-- ### Breaking Changes - ONLY INCLUDE FOR NEW MAJOR version -->

Expand Down
1 change: 1 addition & 0 deletions Realm/RLMRealm.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ - (RLMRealm *)frozenCopy {
try {
RLMRealm *realm = [[RLMRealm alloc] initPrivate];
realm->_realm = _realm->freeze();
realm->_realm->set_schema_subset(_realm->schema());
realm->_realm->read_group();
realm->_dynamic = _dynamic;
realm->_schema = _schema;
Expand Down
33 changes: 33 additions & 0 deletions Realm/Tests/LinkingObjectsTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,39 @@ - (void)testLinkingObjectsOnUnmanagedObject {
@"Linking objects notifications are only supported on managed objects.");
}

- (void)testLinkingObjectsOnFrozenObject {
NSArray *(^asArray)(id) = ^(id arrayLike) {
return [arrayLike valueForKeyPath:@"self"];
};

RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
PersonObject *hannah = [PersonObject createInRealm:realm withValue:@[@"Hannah", @0]];
PersonObject *mark = [PersonObject createInRealm:realm withValue:@[@"Mark", @30, @[hannah]]];
[realm commitWriteTransaction];

PersonObject *frozenHannah = hannah.freeze;
PersonObject *frozenMark = mark.freeze;
XCTAssertEqualObjects(asArray(frozenHannah.parents), (@[frozenMark]));

[realm beginWriteTransaction];
PersonObject *diane = [PersonObject createInRealm:realm withValue:@[@"Diane", @29, @[hannah]]];
[realm commitWriteTransaction];

PersonObject *frozenHannah2 = hannah.freeze;
PersonObject *frozenMark2 = mark.freeze;
PersonObject *frozenDiane = diane.freeze;
XCTAssertEqualObjects(asArray(frozenHannah.parents), (@[frozenMark]));
XCTAssertEqualObjects(asArray(frozenHannah2.parents), (@[frozenMark2, frozenDiane]));

[realm beginWriteTransaction];
[realm deleteObject:hannah];
[realm commitWriteTransaction];

XCTAssertEqualObjects(asArray(frozenHannah.parents), (@[frozenMark]));
XCTAssertEqualObjects(asArray(frozenHannah2.parents), (@[frozenMark2, frozenDiane]));
}

- (void)testFilteredLinkingObjects {
NSArray *(^asArray)(id) = ^(id arrayLike) {
return [arrayLike valueForKeyPath:@"self"];
Expand Down

0 comments on commit 94e356e

Please sign in to comment.