Skip to content

Commit

Permalink
Allow opening synced realm in-memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaafanador3 committed Nov 17, 2023
1 parent 95cb711 commit 1647a19
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
54 changes: 54 additions & 0 deletions Realm/ObjectServerTests/SwiftFlexibleSyncServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,60 @@ extension SwiftFlexibleSyncServerTests {
waitForUploads(for: realm)
}

func testFlexibleSyncInitInMemory() async throws {
try await populateFlexibleSyncData { realm in
for i in 1...5 {
// Using firstname to query only objects from this test
let person = SwiftPerson(firstName: "\(#function)",
lastName: "lastname_\(i)",
age: i)
realm.add(person)
}
}

let user = try await self.flexibleSyncApp.login(credentials: basicCredentials(app: flexibleSyncApp))

try await Task {
var config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
subs.append(QuerySubscription<SwiftPerson> {
$0.age > 0 && $0.firstName == "\(#function)"
})
})
config.objectTypes = [SwiftPerson.self]
config.inMemoryIdentifier = "identifier"
let inMemoryRealm = try await Realm(configuration: config, downloadBeforeOpen: .always)
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 5)
try! inMemoryRealm.write {
let person = SwiftPerson(firstName: "\(#function)",
lastName: "lastname_10",
age: 10)
inMemoryRealm.add(person)
}
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 6)
try await inMemoryRealm.syncSession?.wait(for: .upload)
}.value

var config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
subs.append(QuerySubscription<SwiftPerson> {
$0.age > 5 && $0.firstName == "\(#function)"
})
})
config.objectTypes = [SwiftPerson.self]
config.inMemoryIdentifier = "identifier"
let inMemoryRealm = try await Realm(configuration: config, downloadBeforeOpen: .always)
XCTAssertEqual(inMemoryRealm.objects(SwiftPerson.self).count, 1)

var config2 = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
subs.append(QuerySubscription<SwiftPerson> {
$0.age > 0 && $0.firstName == "\(#function)"
})
})
config2.objectTypes = [SwiftPerson.self]
config2.inMemoryIdentifier = "identifier2"
let inMemoryRealm2 = try await Realm(configuration: config2, downloadBeforeOpen: .always)
XCTAssertEqual(inMemoryRealm2.objects(SwiftPerson.self).count, 6)
}

@MainActor
func populateSwiftPerson() async throws {
try await populateFlexibleSyncData { realm in
Expand Down
6 changes: 3 additions & 3 deletions Realm/RLMRealmConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ typedef void(^RLMFlexibleSyncInitialSubscriptionsBlock)(RLMSyncSubscriptionSet *

#pragma mark - Properties

/// The local URL of the Realm file. Mutually exclusive with `inMemoryIdentifier`;
/// The local URL of the Realm file. Mutually exclusive with `inMemoryIdentifier`,
/// setting one of the two properties will automatically nil out the other.
@property (nonatomic, copy, nullable) NSURL *fileURL;

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

/// A 64-byte key to use to encrypt the data, or `nil` if encryption is not enabled.
Expand Down
2 changes: 0 additions & 2 deletions Realm/RLMRealmConfiguration.mm
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ - (void)setInMemoryIdentifier:(NSString *)inMemoryIdentifier {
if (inMemoryIdentifier.length == 0) {
@throw RLMException(@"In-memory identifier must not be empty");
}
_config.sync_config = nullptr;
_seedFilePath = nil;

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

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

Expand Down
11 changes: 4 additions & 7 deletions RealmSwift/RealmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ extension Realm {
/**
Creates a `Configuration` which can be used to create new `Realm` instances.
- note: The `fileURL`, `inMemoryIdentifier`, and `syncConfiguration` parameters are mutually exclusive. Only
- note: The `fileURL`, `inMemoryIdentifier`, parameters are mutually exclusive. Only
set one of them, or none if you wish to use the default file URL.
Synced Realms will set a unique file path unless is an in-memory realm.
- parameter fileURL: The local URL to the Realm file.
- parameter inMemoryIdentifier: A string used to identify a particular in-memory Realm.
Expand Down Expand Up @@ -106,15 +107,13 @@ extension Realm {
// MARK: Configuration Properties

/**
A configuration value used to configure a Realm for synchronization with Atlas App Services. Mutually
exclusive with `inMemoryIdentifier`.
A configuration value used to configure a Realm for synchronization with Atlas App Services.
*/
public var syncConfiguration: SyncConfiguration? {
get {
return _syncConfiguration
}
set {
_inMemoryIdentifier = nil
_syncConfiguration = newValue
}
}
Expand All @@ -128,15 +127,13 @@ extension Realm {
}
}

/// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL` and
/// `syncConfiguration`.
/// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL`.
public var inMemoryIdentifier: String? {
get {
return _inMemoryIdentifier
}
set {
fileURL = nil
_syncConfiguration = nil
_inMemoryIdentifier = newValue
}
}
Expand Down

0 comments on commit 1647a19

Please sign in to comment.