Let me try to clarify for Richard and Jay. In the first ViewController that is launched after the Launch Screen. I want to access the PersonalData.swift data model and Realm File to grab the user name that is already in the database. This is for a hello greeting on the first screen
let realm = **try** ! Realm()
var users: Results<PersonalData>?
users = realm.objects(PersonalData.self)
nameLabel.text = users?[0].name //Realm retrieved [0] name property
This works fine in the first access on the simulator. But when I try to run it on the iPhone, I get an out of bounds message on the nameLabel.text = line. Robert mentioned that the reason could be because the Realm data may not have actually loaded on the iPhone at the time that I try to load it into nameLabel.text to make it visible on the view that is tied to that ViewController.
On that View, I have a Setup barbutton that looks like a gear. That will segue to the SetupViewController. I have 5 buttons on that View which segue to specific functionally to be maintained. The top button says Personal. By selecting it, I segue to the PersonalViewController and View that contains three TextFields that are loaded from the PersonalViewControllers access of the PersonalData db.
In that view controller I load three properties from the 0 record. There is only one object in the PersonData db. The three that I load are name, email and hireDate. They come in just fine in the simulator.
let realm = try! Realm()
var users: Results<PersonalData>?
// Realm retrieved [0] properties from PersonalData.swift
users = realm.objects(PersonalData.self)
screenNameTextField.text = users?[0].name
emailAddressTextField.text = users?[0].email
hireDateTextField.text = users?[0].hireDate
These load just fine. Richard gave me some suggestions about ways to watch for a notification that the data has actually loaded on the iPhone before I try to use them. I have not done that development yet but that is next on my agenda to make sure I can load the original request as works in the simulator.
Here is my PersonalData.swift file. Please excuse the * that get loaded in the cut&paste.
**import** Foundation
**import** RealmSwift
**class** PersonalData: Object {
**@objc** **dynamic** **var** personalID: Int = 0
**@objc** **dynamic** **var** name: String = "" // Mike Granberry
**@objc** **dynamic** **var** email: String = "" // mgranbe@gmail.com
**@objc** **dynamic** **var** hireDate: String = "" // mm/dd/yyyy of first year
**let** businessYear = List<YearData>()
**let** anniversaryYear = List<AnniversaryData>()
**override** **class** **func** primaryKey() -> String? {
return "personalID"
}
}
I hope this helps the conversation. I am very appreciative. As you can tell I am very new at this language and trying to do some pretty bold object database access as the app grows. Thanks.