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
Swift: Lazily Encapsulating Chains of Map, Filter, Flatmap
Swift and Objectmapper: Nsdate with Min Value
Float Is Not Convertible to 'Mirrordisposition' Swift What Is Mirrordisposition
How Does Let X Where X.Hassuffix("Pepper") Work
Com.Apple.Itunes Aedeterminepermissiontoautomatetarget Is Always Return -600
How to Check Whether an Object Is Kind of a Dynamic Class Type in Swift
Swift: Hashable Struct with Dictionary Property
Changing Nav Bar Item Programmatically in Swift
How to Test an Optionset with a Switch Statement
Testing Protocol Conformance with Associated Types
Nspopover to Start in a Detached State
Not Getting Expected Delegate Calls When Trying to Restore In-App Purchases with Storekit
How to Keep a Reference to Another Object in the Parameters of the Class
Cloudkit Ckqueryoperation Doesn't Get All Records
How to Add Floating Button on Top of the Uitableview
Combined Chart (Line- and Bar Chart) Using iOS-Charts
iOS Swift Error: 'T' Is Not Convertible to 'Mirrordisposition'