A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/swift/sync/partition-based-sync/ below:

Partition-Based Sync - Swift SDK - Atlas Device SDKs

Partition-Based Sync is an older mode for using Atlas Device Sync with the Realm Swift SDK. We recommend using Flexible Sync for new apps. The information on this page is for users who are still using Partition-Based Sync.

For more information about Partition-Based Sync and how to configure it in Atlas App Services, refer to Partition-Based Sync in the App Services documentation.

When you select Partition-Based Sync for your backend App configuration, your client implementation must include a partition value. This is the value of the partition key field you select when you configure Partition-Based Sync.

The partition value determines which data the client application can access.

You pass in the partition value when you open a synced realm.

Initialize a synced realm with a sync configuration. This enables you to specify a partition value whose data should sync to the realm.

The first time you log in and open a synced realm, you'll log in the user, and pass the user's RLMSyncConfiguration object with the desired partitionValue to +[RLMRealm realmWithConfiguration:error:].

This opens a synced realm on the device. The realm attempts to sync with your App in the background to check for changes on the server, or upload changes that the user has made.

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];RLMUser *user = [app currentUser];NSString *partitionValue = @"some partition value";RLMRealmConfiguration *configuration = [user configurationWithPartitionValue:partitionValue];NSError *error = nil;RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration                                             error:&error];if (error != nil) {    NSLog(@"Failed to open realm: %@", [error localizedDescription]);    } else {    NSLog(@"Opened realm: %@", realm);    }

Pass a logged-in user's configuration object with the desired partition value to realm initializers.

You can optionally specify whether a realm should download changes before opening. If you do not specify download behavior, this opens a realm with data that is on the device, and attempts to sync changes in the background.

let app = App(id: YOUR_APP_SERVICES_APP_ID)let user = app.currentUserlet partitionValue = "some partition value"var configuration = user!.configuration(partitionValue: partitionValue)let syncedRealm = try! Realm(configuration: configuration)print("Successfully opened the synced realm: \(syncedRealm)")

New in version 10.23.0.

If you want a non-synced realm to start syncing with other devices and your App Services backend, you can use the writeCopy(configuration: ) method to make a copy of the non-synced realm for use with a sync configuration. The example below creates a copy of a non-synced realm file, with all of its existing data, that you can use with a sync configuration.

After you copy the realm for use with Sync, you can open the copy as a synced realm. Any changes you make to the synced realm will reflect in the synced realm file, and they will also propogate to other devices and the App Services backend.

Note Partition-Based Sync Only

This method only supports converting between a non-sync realm and Partition-Based Sync. If your app uses Flexible Sync, you must manually iterate through the objects in one realm and copy them into the other realm.

Tip

If your app accesses Realm in an async/await context, mark the code with @MainActor to avoid threading-related crashes.

try await convertLocalRealmToSyncedRealm()@MainActorfunc convertLocalRealmToSyncedRealm() async throws {    let app = App(id: YOUR_APP_SERVICES_APP_ID)            let syncUser = try await app.login(credentials: Credentials.anonymous)            var syncConfig = syncUser.configuration(partitionValue: "Your Partition Value")    syncConfig.objectTypes = [QsTask.self]            var localConfig = Realm.Configuration()    localConfig.objectTypes = [QsTask.self]                    let localRealm = addExampleData(config: localConfig)                    try! localRealm.writeCopy(configuration: syncConfig)            let syncedRealm = try await Realm(configuration: syncConfig)                let syncedTasks = syncedRealm.objects(QsTask.self)    var frodoSyncedTasks = syncedTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoSyncedTasks.count, 3)    print("Synced realm opens and contains this many tasks: \(frodoSyncedTasks.count)")            let task4 = QsTask(value: ["name": "Send gift basket to Tom Bombadil", "owner": "Frodo"])        try! syncedRealm.write {        syncedRealm.add(task4)    }        frodoSyncedTasks = syncedTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoSyncedTasks.count, 4)    print("After adding a task, the synced realm contains this many tasks: \(frodoSyncedTasks.count)")            let openedLocalRealm = try await Realm(configuration: localConfig)    let localTasks = openedLocalRealm.objects(QsTask.self)    let frodoLocalTasks = localTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoLocalTasks.count, 3)    print("Local realm opens and contains this many tasks: \(frodoLocalTasks.count)")        XCTAssertNotEqual(frodoLocalTasks.count, frodoSyncedTasks.count)                func addExampleData(config: Realm.Configuration) -> Realm {                        let localConfig = config                let localRealm = try! Realm(configuration: localConfig)                let task1 = QsTask(value: ["name": "Keep it secret", "owner": "Frodo"])        let task2 = QsTask(value: ["name": "Keep it safe", "owner": "Frodo"])        let task3 = QsTask(value: ["name": "Journey to Bree", "owner": "Frodo"])                try! localRealm.write {            localRealm.add([task1, task2, task3])        }        return localRealm    }}

New in version 10.23.0.

If you want to permanently stop a realm from syncing to your App Services backend, you can use the writeCopy(configuration: ) method to make a copy of a synced realm for use with a non-sync configuration. The example below creates a copy of the realm file, with all of its existing data, at a file URL you specify.

This process removes the realm_id in the local realm. You must increment the schema version as if you had deleted a property.

After you copy the realm for use without Sync, you can open the copy as a non-synced realm. Any changes you make to the non-synced realm reflect only in the local realm file. No changes propogate to other devices or the App Services backend.

Note Partition-Based Sync Only

This method only supports converting between a non-sync realm and Partition-Based Sync. If your app uses Flexible Sync, you must manually iterate through the objects in one realm and copy them into the other realm.

Tip

If your app accesses Realm in an async/await context, mark the code with @MainActor to avoid threading-related crashes.

try await convertSyncedRealmToLocalRealm()@MainActorfunc convertSyncedRealmToLocalRealm() async throws {    let app = App(id: YOUR_APP_SERVICES_APP_ID)            let syncUser = try await app.login(credentials: Credentials.anonymous)            var syncConfig = syncUser.configuration(partitionValue: "Some Partition Value")    syncConfig.objectTypes = [QsTask.self]                let syncedRealm = try await Realm(configuration: syncConfig, downloadBeforeOpen: .always)    print("Successfully opened realm: \(syncedRealm)")                let syncedTasks = syncedRealm.objects(QsTask.self)    var frodoSyncedTasks = syncedTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoSyncedTasks.count, 3)    print("Synced realm opens and contains this many tasks: \(frodoSyncedTasks.count)")            guard let outputDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }            let localRealmFilePath = outputDir.appendingPathComponent("local.realm")            var localConfig = Realm.Configuration()    localConfig.objectTypes = [QsTask.self]    localConfig.fileURL = localRealmFilePath                localConfig.schemaVersion = 1                if Realm.fileExists(for: localConfig) {        try Realm.deleteFiles(for: localConfig)        print("Successfully deleted existing realm at path: \(localRealmFilePath)")    } else {        print("No file currently exists at path")    }            try syncedRealm.writeCopy(configuration: localConfig)            let localRealm = try await Realm(configuration: localConfig)            let localTasks = localRealm.objects(QsTask.self)    var frodoLocalTasks = localTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoLocalTasks.count, 3)    print("Local realm opens and contains this many tasks: \(frodoLocalTasks.count)")        let task = QsTask(value: ["name": "Send gift basket to Tom Bombadil", "owner": "Frodo"])        try! localRealm.write {        localRealm.add(task)    }        frodoLocalTasks = localTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoLocalTasks.count, 4)    print("After adding a task, the local realm contains this many tasks: \(frodoLocalTasks.count)")        frodoSyncedTasks = syncedTasks.where { $0.owner == "Frodo" }    XCTAssertEqual(frodoSyncedTasks.count, 3)    print("After writing to local realm, synced realm contains this many tasks: \(frodoSyncedTasks.count)")        XCTAssertNotEqual(frodoLocalTasks.count, frodoSyncedTasks.count)}

You can migrate your App Services Device Sync Mode from Partition-Based Sync to Flexible Sync. Migrating is an automatic process that does not require any changes to your application code. Automatic migration requires Realm Swift SDK version 10.40.0 or newer.

Migrating enables you to keep your existing App Services users and authentication configuration. Flexible Sync provides more versatile permissions configuration options and more granular data synchronization.

For more information about how to migrate your App Services App from Partition-Based Sync to Flexible Sync, refer to Migrate Device Sync Modes.

The automatic migration from Partition-Based Sync to Flexible Sync does not require any changes to your client code. However, to support this functionality, Realm automatically handles the differences between the two Sync Modes by:

If you need to make updates to your client code after migration, consider updating your client codebase to remove hidden migration functionality. You might want update your client codebase when:

Make these changes to convert your Partition-Based Sync client code to use Flexible Sync:

For examples of Flexible Sync permissions strategies, including examples of how to model data for these strategies, refer to Device Sync Permissions Guide.

When you migrate from Partition-Based Sync to Flexible Sync, Realm automatically creates hidden Flexible Sync subscriptions for your app. The next time you add or change subscriptions, we recommend that you:

  1. Remove the automatically-generated subscriptions.

  2. Manually add the relevant subscriptions in your client codebase.

This enables you to see all of your subscription logic together in your codebase for future iteration and debugging.

For more information about the automatically-generated Flexible Sync subscriptions, refer to Migrate Client App to Flexible Sync.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4