I created an embeddedObject containing many fields that are present if excel as column headings (which are the key), and values are the values in each row accordingly. A list of these embeddedObjects was then created, having an array of embeddedObject so as to contain each day’s data. The object had a date field where a string was given, and an ObjectId as well as the list of these embeddedObjects. Flexible sync I tried, but in documentation it is mentioned that it requires(prerequisites) non shared MongoDB Atlas cluster running MongoDB 5.0 or greater. Also created an ObservedResults variable to get the results for the frontend. While opening the frontend, I also gave an environment after checking if user is logged in. I have also tried partition based sync, which apparently did not work for this project, although it did for another one.
Here are the files:
RealmDemoApp.Swift:
import SwiftUI
import Realm
import RealmSwift
let realmApp = RealmSwift.App(id: "xxxxxx")
@main
struct RealmDemoApp: SwiftUI.App {
var body: some Scene {
WindowGroup {
if UserDefaults.standard.bool(forKey: "ISUSERLOGGEDIN") == true {
if let user = realmApp.currentUser {
DetailView(model: DemoModel())
.environment(\.realmConfiguration, user.configuration(partitionValue: "demo"))
}
}
else {
LoginView()
}
}
}
}
LoginView.swift:
import SwiftUI
import RealmSwift
struct LoginView: View {
@State var email: String = ""
@State var password: String = ""
@State private var isSecured: Bool = true
let realm = try! Realm()
var body: some View {
NavigationView {
VStack(alignment: .center, spacing: 30) {
Spacer()
Text("Login")
.font(.largeTitle)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
VStack(spacing: 20){
VStack(alignment: .leading){
HStack(){
Text("Email")
.font(.title)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Spacer()
}
TextField("Email", text: $email)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
VStack(alignment: .leading){
HStack(){
Text("Password")
.font(.title)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Spacer()
}
ZStack(alignment: .trailing, content: {
Group {
if isSecured {
SecureField("Password", text: $password)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
else {
TextField("Password", text: $password)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
}
Button {
isSecured.toggle()
} label: {
Image(systemName: self.isSecured ? "eye.slash" : "eye")
.accentColor(.gray)
.padding(20)
}
})
}
NavigationLink {
if UserDefaults.standard.bool(forKey: "ISUSERLOGGEDIN") == true {
if let user = realmApp.currentUser {
DetailView(model: DemoModel())
.environment(\.realmConfiguration, user.configuration(partitionValue: "pnl"))
}
else {
Text("login view 84th line")
}
}
} label: {
Button {
if(email.isEmpty && password.isEmpty){
// RealmAuthAnonymous()
print("enter email and password")
}
else {
RealmAuth(email: email, password: password)
}
} label: {
Text("Sign In")
}
.cornerRadius(10)
.frame(width: UIScreen.main.bounds.width/1.5, height: 50)
.background(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.onTapGesture {
if UserDefaults.standard.bool(forKey: "ISUSERLOGGEDIN") == true {
UserDefaults.standard.set(email, forKey: "email")
print("email is: \($email)")
}
}
}
.navigationBarBackButtonHidden(true)
HStack{
Button {
} label: {
Text("Forgot Password")
.underline()
.font(.caption)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
}
}
HStack{
Text("Don't have an account?")
.font(.caption)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
NavigationLink {
SignUpView()
} label: {
Text("Sign Up")
.underline()
.font(.title2)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
}
}
}
Text("or")
.font(.caption)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Spacer()
}
.padding()
.edgesIgnoringSafeArea(.all)
.background(.black)
}
// func login(){
// Task {
// do{
// let user = try await app.login(credentials: .anonymous)
// username = user.id
// } catch {
// print("Failed to login: \(error.localizedDescription)")
// }
// }
// }
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
SignUpView.swift:
import SwiftUI
struct SignUpView: View {
@State var email: String = ""
@State var password: String = ""
@State private var isSecured: Bool = true
// let userDefaults = UserDefaults.standard
var body: some View {
VStack(alignment: .center, spacing: 30) {
Spacer()
Text("Sign Up")
.font(.largeTitle)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
VStack(spacing: 20){
VStack(alignment: .leading){
HStack(){
Text("Email")
.font(.title)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Spacer()
}
TextField("Email", text: $email)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
VStack(alignment: .leading){
HStack(){
Text("Password")
.font(.title)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Spacer()
}
ZStack(alignment: .trailing, content: {
Group {
if isSecured {
SecureField("Password", text: $password)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
else {
TextField("Password", text: $password)
.textFieldStyle(.roundedBorder)
.border(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
.cornerRadius(7)
}
}
Button {
isSecured.toggle()
} label: {
Image(systemName: self.isSecured ? "eye.slash" : "eye")
.accentColor(.gray)
.padding(20)
}
})
}
Button {
if(email.isEmpty && password.isEmpty){
// RealmAuthAnonymous()
print("enter email and password")
}
else {
RealmRegister(email: email, password: password)
}
} label: {
Text("Sign Up")
}
.cornerRadius(10)
.frame(width: UIScreen.main.bounds.width/1.5, height: 50)
.background(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
}
Text("or")
.font(.caption)
.bold()
.foregroundColor(Color(.init(srgbRed: 255, green: 215, blue: 0, alpha: 0.9)))
Button {
//
} label: {
Image("google")
.clipShape(Circle())
}
Spacer()
}
.padding()
.edgesIgnoringSafeArea(.all)
.background(.black)
}
}
struct SignUpView_Previews: PreviewProvider {
static var previews: some View {
SignUpView()
}
}
DetailView.swift:
import SwiftUI
import Realm
import RealmSwift
struct DetailView: View {
@ObservedRealmObject var model: DemoModel
@State var busy = false
var body: some View {
ZStack{
VStack {
List {
Text("entry")
.onAppear(){
print(model)
}
}
}
.padding()
if busy {
ProgressView()
}
}
.onChange(of: model) { newValue in
print("on change: ")
print(model)
}
}
}
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
DetailView(model: DemoModel())
}
}
DemoModel.swift:
import Foundation
import RealmSwift
class DemoModel: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId?
@Persisted var SubModel: List<DemoModel_SubModel>
@Persisted var date: String?
override static func primaryKey() -> String? {
return "_id"
}
convenience init(SubModel: List<DemoModel_SubModel>, date: String?) {
self.init()
self.SubModel = SubModel
self.date = date
}
}
DemoModel_SubModel.swift:
import Foundation
import RealmSwift
class DemoModel_SubModel: EmbeddedObject, ObjectKeyIdentifiable {
@Persisted var field1: Int?
@Persisted var field2: Int?
@Persisted var field3: Int?
@Persisted var field4: Int?
@Persisted var field5: Int?
@Persisted var field6: Int?
@Persisted var field7: Int?
@Persisted var field8: Int?
@Persisted var field9: Int?
@Persisted var field10: Int?
@Persisted var field11: Int?
@Persisted var field12: Int?
@Persisted var field13: Int?
@Persisted var field14: Int?
@Persisted var field15: Int?
@Persisted var field16: Int?
@Persisted var field17: Int?
@Persisted var field18: Int?
@Persisted var field19: Int?
@Persisted var field20: Int?
@Persisted var field21: String?
@Persisted var field22: Int?
@Persisted var field23: Int?
@Persisted var field24: Int?
@Persisted var field25: Int?
@Persisted var field26: Int?
@Persisted var field27: Int?
@Persisted var field28: Int?
@Persisted var field29: Int?
@Persisted var field30: Int?
@Persisted var field31: Int?
@Persisted var field32: Int?
convenience init(field1: Int?, field2: Int?, field3: Int?, field4: Int?, field5: Int?, field6: Int?, field7: Int?, field8: Int?, field9: Int?, field10: Int?, field11: Int?, field12: Int?, field13: Int?, field14: Int?, field15: Int?, field16: Int?, field17: Int?, field18: Int?, field19: Int?, field20: Int?, field21: String?, field22: Int?, field23: Int?, field24: Int?, field25: Int?, field26: Int?, field27: Int?, field28: Int?, field29: Int?, field30: Int?, field31: Int?, field32: Int?) {
self.init()
self.field1 = field1
self.field2 = field2
self.field3 = field3
self.field4 = field4
self.field5 = field5
self.field6 = field6
self.field7 = field7
self.field8 = field8
self.field9 = field9
self.field10 = field10
self.field11 = field11
self.field12 = field12
self.field13 = field13
self.field14 = field14
self.field15 = field15
self.field16 = field16
self.field17 = field17
self.field18 = field18
self.field19 = field19
self.field20 = field20
self.field21 = field21
self.field22 = field22
self.field23 = field23
self.field24 = field24
self.field25 = field25
self.field26 = field26
self.field27 = field27
self.field28 = field28
self.field29 = field29
self.field30 = field30
self.field31 = field31
self.field32 = field32
}
}
Constants.swift:
import Foundation
import Realm
import RealmSwift
func RealmRegister(email: String, password: String){
let client = realmApp.emailPasswordAuth
client.registerUser(email: email, password: password){ (error) in
guard error == nil else {
print("Failed to register: \(error!.localizedDescription)")
return
}
print("successfully registered user")
}
}
func RealmAuth(email: String, password: String){
realmApp.login(credentials: Credentials.emailPassword(email: email, password: password)) { (result) in
switch result {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
case .success(let user):
UserDefaults.standard.set(true, forKey: "ISUSERLOGGEDIN")
print("Successfully logged in as user \(user)")
}
}
}
func RealmAuthAnonymous() {
let anonymousCredentials = Credentials.anonymous
realmApp.login(credentials: anonymousCredentials){ (result) in
switch result {
case .failure(let error):
print("Anonymous Login failed: \(error.localizedDescription)")
case .success(let user):
UserDefaults.standard.set(true, forKey: "ISUSERLOGGEDIN")
print("Successfully anonymously logged in as user \(user)")
}
}
}
I have given Database access as read and write to any database, network access as 0.0.0.0/0, defined Rules as readAndWriteAll, generated Schema from data as:
{
"title": "DemoModel",
"properties": {
"SubModel": {
"bsonType": "array",
"items": {
"bsonType": "object",
"properties": {
"field1": {
"bsonType": "int"
},
"field10": {
"bsonType": "int"
},
"field11": {
"bsonType": "int"
},
"field12": {
"bsonType": "int"
},
"field13": {
"bsonType": "int"
},
"field14": {
"bsonType": "int"
},
"field15": {
"bsonType": "int"
},
"field16": {
"bsonType": "int"
},
"field17": {
"bsonType": "int"
},
"field18": {
"bsonType": "int"
},
"field19": {
"bsonType": "int"
},
"field2": {
"bsonType": "int"
},
"field20": {
"bsonType": "int"
},
"field21": {
"bsonType": "string"
},
"field22": {
"bsonType": "int"
},
"field23": {
"bsonType": "int"
},
"field24": {
"bsonType": "int"
},
"field25": {
"bsonType": "int"
},
"field26": {
"bsonType": "int"
},
"field27": {
"bsonType": "int"
},
"field28": {
"bsonType": "int"
},
"field29": {
"bsonType": "int"
},
"field3": {
"bsonType": "int"
},
"field30": {
"bsonType": "int"
},
"field31": {
"bsonType": "int"
},
"field32": {
"bsonType": "int"
},
"field4": {
"bsonType": "int"
},
"field5": {
"bsonType": "int"
},
"field6": {
"bsonType": "int"
},
"field7": {
"bsonType": "int"
},
"field8": {
"bsonType": "int"
},
"field9": {
"bsonType": "int"
}
}
}
},
"_id": {
"bsonType": "objectId"
},
"date": {
"bsonType": "string"
}
}
}
DemoModel.swift and DemoModel_SubModel.swift have been replicated as suggested by RealmObjectModels.
I have also enabled Partition-based Device Sync, and the Developer Mode is on.
The database looks like this:
I am getting stuck somewhere still, as the results show this:
This project is at urgent priority, would be appreciative if somebody could help where I am getting it wrong.