I have a Realm database which holds records for different users with distinct userID's.
I also have a UITableViewController which displays the results of a Realm query. I would like the query to return only the Passages for the current user.
class PassageListController: UITableViewController {
var currentUserID: Int = NSUserDefaults.standardUserDefaults().objectForKey("currentUserID") as! Int
lazy var dataArray: Results<Passage> = { return try! Realm().objects(Passage).filter("userID == \(currentUserID)")}
The problem is that I'm getting the following error message:
Instance member 'currentUserID' cannot be used on type 'PassageListController'
This is despite trying to lazily load the Realm query. What is the best practice method to solve this problem?
Anticipating some solutions:
- I can't load the query in viewDidAppear() as there is no way to define an empty Realm Result and it needs to be accessible throughout the controller.
- I could set the currentUserID as a global variable but that violates my principles.