Skip to content

Commit 1ef89df

Browse files
Allow opening in-memory synced realms. (#8421)
1 parent 6a0054a commit 1ef89df

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ x.y.z Release notes (yyyy-MM-dd)
1010
* The `baseURL` field of `AppConfiguration` can now be updated, rather than the
1111
value being persisted between runs of the application in the metadata
1212
storage. ([Core #7201](https://github.com/realm/realm-core/issues/7201))
13+
* Allow in-memory synced Realms. This will allow setting an in-memory identifier on
14+
a flexible sync realm.
1315

1416
### Fixed
1517
* `@Persisted`'s Encodable implementation did not allow the encoder to
@@ -9393,7 +9395,7 @@ Prebuilt frameworks are now built with Xcode 7.1.
93939395
the properties in a `RLMObject` subclass.
93949396
* Fix crash on IN query with several thousand items.
93959397
* Fix crash when querying indexed `NSString` properties.
9396-
* Fixed an issue which prevented in-memory Realms from being used accross multiple threads.
9398+
* Fixed an issue which prevented in-memory Realms from being used across multiple threads.
93979399
* Preserve the sort order when querying a sorted `RLMResults`.
93989400
* Fixed an issue with migrations where if a Realm file is deleted after a Realm is initialized,
93999401
the newly created Realm can be initialized with an incorrect schema version.

Realm/ObjectServerTests/AsyncSyncTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,52 @@ class AsyncFlexibleSyncTests: SwiftSyncTestCase {
514514
checkCount(expected: 10, realm, SwiftPerson.self)
515515
}
516516

517+
@MainActor
518+
func testFlexibleSyncInitInMemory() async throws {
519+
try await populateSwiftPerson(5)
520+
521+
let user = try await createUser()
522+
try await Task {
523+
var config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
524+
subs.append(QuerySubscription<SwiftPerson> {
525+
$0.age > 0 && $0.firstName == self.name
526+
})
527+
})
528+
config.objectTypes = [SwiftPerson.self]
529+
config.inMemoryIdentifier = "identifier"
530+
let inMemoryRealm = try await Realm(configuration: config, downloadBeforeOpen: .always)
531+
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 5)
532+
try! inMemoryRealm.write {
533+
let person = SwiftPerson(firstName: self.name,
534+
lastName: "lastname_10",
535+
age: 10)
536+
inMemoryRealm.add(person)
537+
}
538+
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 6)
539+
try await inMemoryRealm.syncSession?.wait(for: .upload)
540+
}.value
541+
542+
var config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
543+
subs.append(QuerySubscription<SwiftPerson> {
544+
$0.age > 5 && $0.firstName == self.name
545+
})
546+
})
547+
config.objectTypes = [SwiftPerson.self]
548+
config.inMemoryIdentifier = "identifier"
549+
let inMemoryRealm = try await Realm(configuration: config, downloadBeforeOpen: .always)
550+
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 1)
551+
552+
var config2 = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
553+
subs.append(QuerySubscription<SwiftPerson> {
554+
$0.age > 0 && $0.firstName == self.name
555+
})
556+
})
557+
config2.objectTypes = [SwiftPerson.self]
558+
config2.inMemoryIdentifier = "identifier2"
559+
let inMemoryRealm2 = try await Realm(configuration: config2, downloadBeforeOpen: .always)
560+
XCTAssertEqual(inMemoryRealm2.objects(SwiftPerson.self).count, 6)
561+
}
562+
517563
@MainActor
518564
func testStates() async throws {
519565
let realm = try await openRealm()

Realm/RLMRealmConfiguration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ typedef void(^RLMFlexibleSyncInitialSubscriptionsBlock)(RLMSyncSubscriptionSet *
8080
@property (nonatomic, copy, nullable) NSURL *fileURL;
8181

8282
/// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL`,
83-
/// `seedFilePath`and `syncConfiguration`;
84-
/// setting any one of the three properties will automatically nil out the other two.
83+
/// `seedFilePath`.
84+
/// setting an in-memory identifier will automatically nil out the other two.
8585
@property (nonatomic, copy, nullable) NSString *inMemoryIdentifier;
8686

8787
/// A 64-byte key to use to encrypt the data, or `nil` if encryption is not enabled.

Realm/RLMRealmConfiguration.mm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ - (void)setInMemoryIdentifier:(NSString *)inMemoryIdentifier {
174174
if (inMemoryIdentifier.length == 0) {
175175
@throw RLMException(@"In-memory identifier must not be empty");
176176
}
177-
_config.sync_config = nullptr;
178177
_seedFilePath = nil;
179178

180179
RLMNSStringToStdString(_config.path, [NSTemporaryDirectory() stringByAppendingPathComponent:inMemoryIdentifier]);
@@ -367,7 +366,6 @@ - (void)setSyncConfiguration:(RLMSyncConfiguration *)syncConfiguration {
367366
}
368367

369368
NSAssert(user.identifier, @"Cannot call this method on a user that doesn't have an identifier.");
370-
_config.in_memory = false;
371369
_config.sync_config = std::make_shared<realm::SyncConfig>(syncConfiguration.rawConfiguration);
372370
_config.path = syncConfiguration.path;
373371

RealmSwift/RealmConfiguration.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ extension Realm {
5151
/**
5252
Creates a `Configuration` which can be used to create new `Realm` instances.
5353

54-
- note: The `fileURL`, `inMemoryIdentifier`, and `syncConfiguration` parameters are mutually exclusive. Only
54+
- note: The `fileURL`, and `inMemoryIdentifier`, parameters are mutually exclusive. Only
5555
set one of them, or none if you wish to use the default file URL.
56+
Synced Realms will set a unique file path unless is an in-memory realm.
5657

5758
- parameter fileURL: The local URL to the Realm file.
5859
- parameter inMemoryIdentifier: A string used to identify a particular in-memory Realm.
@@ -106,15 +107,13 @@ extension Realm {
106107
// MARK: Configuration Properties
107108

108109
/**
109-
A configuration value used to configure a Realm for synchronization with Atlas App Services. Mutually
110-
exclusive with `inMemoryIdentifier`.
110+
A configuration value used to configure a Realm for synchronization with Atlas App Services.
111111
*/
112112
public var syncConfiguration: SyncConfiguration? {
113113
get {
114114
return _syncConfiguration
115115
}
116116
set {
117-
_inMemoryIdentifier = nil
118117
_syncConfiguration = newValue
119118
}
120119
}
@@ -128,15 +127,13 @@ extension Realm {
128127
}
129128
}
130129

131-
/// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL` and
132-
/// `syncConfiguration`.
130+
/// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL`.
133131
public var inMemoryIdentifier: String? {
134132
get {
135133
return _inMemoryIdentifier
136134
}
137135
set {
138136
fileURL = nil
139-
_syncConfiguration = nil
140137
_inMemoryIdentifier = newValue
141138
}
142139
}

0 commit comments

Comments
 (0)