I would like to copy data from the local realm to the synced realm only if the logged in user was just created.
struct MainScreen: View {
@EnvironmentObject var app : RealmSwift.App
@ObservedResults(Item.self) var items
var body: some View {
NavigationStack {
}
.onAppear {
// move local to sync
if let localRealm = try? Realm(), let user = app.currentUser {
if items.count == 0 { // if user.isNew
let localItems = localRealm.objects(Item.self)
for item in localItems {
// local -> synced
let syncedItem = Item(value: item)
syncedItem.userId = user.id
$items.append(syncedItem)
// delete local
try? localRealm.write {
localRealm.delete(item)
}
}
}
}
}
}
}
Is there a way to tell if a user logged in for the first time?