Synchronized Realm - Airplane Mode

What primary key to set on local android db before syncing it to server?

Just spitballing...

Add an int local_id and int device_id to the Item class. Every device that interacts with the server will get a unique and persistent device_id. When an Item is created on a device absent an internet connection, the device can refer to the local_id if the item needs to be edited. When the device is connected and an Item (!isSynced && id == null), send the Item to the DB for an insert operation, and then find the primary key of the newly inserted DB row by searching for local_id and device_id. The primary key value is sent back to the device, and the Item's primary key on the server is set as the id on the local copy of Item and set isSynced to true.

ETA: When retrieving Items from the db server that were created on other devices, you would need omit the foreign local_id when putting these Items into the device's table.

ETA2: And you would need to change the table setup on your devices so that local_id was the primary key and id was UNIQUE. But of course id remains the primary key on table on the db server.

Is Realm smart when updating values or check for new values should be perfomed manually?

Your observers will be notified if you update a property on a realm object with the same value. Realm does not care if you use a different value or not.

I'm not sure what your use case is, but it may be a pain in the butt to check every value manually.

You can do something like this though:

protocol UniqueUpdating { }

extension UniqueUpdating where Self: AnyObject {

@discardableResult
func update<Value: Equatable>(
_ keyPath: ReferenceWritableKeyPath<Self, Value>,
to value: Value
) -> Bool {
guard self[keyPath: keyPath] != value else { return false }
self[keyPath: keyPath] = value
return true
}
}

extension Object: UniqueUpdating {}

class Person: Object {
@Persisted(primaryKey: true) var id: Int = 0
@Persisted var name: String = ""
}

Usage would be like this:

let realm = try! Realm()
try! realm.write {
person.update(\.name, to: "BOB")
}

Can't create a MongoDB Realm Sync in Development Mode

OK, so I didn't realize this, but you can type into the dropdown box. So even though the drop down options say "No options", I can type in a partition key name "_partition" (or whatever). This is from a MongoDB person:

... On that screen, instead of using the dropdown, you can type in whatever field name you intend to use for the partition value. We generally use "_partition".



Related Topics



Leave a reply



Submit