I’m using Flexible Sync to develop my first App (I am a cardiologist, excuse me If anything sounds very rudimentary!)
I have successfully managed to authenticate users vie email password authentication and also update their custom data.
my data models ((some of them) - using Chat application by Andrew Morgan as guide):
@Persisted(primaryKey: true) var _id = UUID().uuidString
@Persisted var userName = app.currentUser?.profile.email
@Persisted var firstName = ""
@Persisted var lastName = ""
@Persisted var userRole = ""
@Persisted var userMobile = ""
@Persisted var userCentre = ""
@Persisted var userCentreId: Centre?
@Persisted var userPreferences: UserPreferences?
@Persisted var lastSeenAt: Date?
@Persisted var conversations = List<Conversation>()
@Persisted var presence = "On-Line"
var isProfileSet: Bool { !(userPreferences?.isEmpty ?? true) }
var presenceState: Presence {
get { return Presence(rawValue: presence) ?? .hidden }
set { presence = newValue.asString }
}
convenience init(userName: String, id: String) {
self.init()
self.userName = userName
_id = id
userPreferences = UserPreferences()
userPreferences?.displayName = userName
presence = "On-Line"
}
}
enum Presence: String {
case onLine = "On-Line"
case offLine = "Off-Line"
case hidden = "Hidden"
var asString: String {
self.rawValue
}
}
-------
class Centre:Object, ObjectKeyIdentifiable {
@Persisted var _id: ObjectId = ObjectId.generate()
@Persisted var centreName = ""
@Persisted var centreDesc = ""
@Persisted var centreLocation: Coordinates?
override static func primaryKey() -> String? {
return "_id"
}
convenience init(centreName: String, centreDesc: String, centreLocation: Coordinates) {
self.init()
self.centreName = centreName
self.centreDesc = centreDesc
self.centreLocation = centreLocation
}
}
------
class Coordinates: EmbeddedObject, ObjectKeyIdentifiable {
@Persisted var x: Double?
@Persisted var y: Double?
}
I have created two users and accordingly I have two realm files on disk( under their respective ‘id’s’ folder), which show their profile data and so does Atlas in the ‘User’ collection
Problem is when I add centre details -
If I use the following code to add Centre details, it shows up in the default .realm file (third file - other than the two mentioned above) and does not write to Atlas collection ‘Centre’
func addCentre() {
let longD = Double(long) ?? 0.0
let latD = Double(lat) ?? 0.0
let realm = try! realm
try! realm.write {
let centreLocation = Coordinates()
centreLocation.x = longD
centreLocation.y = latD
let centre = Centre(centreName: centreName, centreDesc: centreDesc, centreLocation: centreLocation)
realm.add(centre)
print("success")
}
and If I use this code it writes to Atlas collection but not to realm files on disk -
func editCentre() {
let longD = Double(long) ?? 0.0
let latD = Double(lat) ?? 0.0
let centreLocation = Coordinates()
centreLocation.x = longD
centreLocation.y = latD
let user = app.currentUser!
let client = user.mongoClient("mongodb-atlas")
let database = client.database(named: "ACC5DB")
let collection = database.collection(withName: "Centre")
// Insert the custom user data object
collection.insertOne([
"_id": AnyBSON(ObjectId.generate()),
"centreName": AnyBSON(centreName),
"centreDesc": AnyBSON(centreDesc),
"centreLocation": AnyBSON(centreLocation)
]) { (result) in
switch result {
case .failure(let error):
print("Failed to insert document: \(error.localizedDescription)")
case .success(let newObjectId):
print("Inserted custom user data document with object ID: \(newObjectId)")
}
}
}
the above code is actually not writing because of error " Initializer ‘init(_:)’ requires that ‘Coordinates’ conform to ‘BSON’" but is writing if I do " centreLocation.x and centreLocation.y but that creates two new fields by the same name in Atlas (not what I want , though), but will figure it out!!
My question is the behaviour of realm writes vs Atlas writes, How do I ensure writes to both?
My Realm files on disk -