Chris
Chris

Reputation: 53

Unable to set properties of Realm database object in Detail View because of nil optional value

I have a realm database with Client objects that each have their own Document list. In my SplitViewController I have a tableview that displays all added clients. When a client is clicked I can display all documents for that specific client in another tableview. The problem that occurs is when clicking a document the app crashes and the details view controller gives the following error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

I've tried troubleshooting this issue and did not have this issue with a master-detail screen in another part of the app so I feel this issue is occurring either because of the two tableviews or because of some issue with the Realm database.

One strange thing is I get the same fatal error even when I assign the value of a label myself without using values from my database.

Unexpected Error

Client.swift

import Foundation
import RealmSwift

class Client: Object {

@objc dynamic var name: String = ""
@objc dynamic var age: Int = 0
@objc dynamic var bond: Double = 0.0
@objc dynamic var continuances: Int = 0

//Realm syntax to define a to-many relationship meaning each Client can have a number of Document objects
let documents = List<Document>()

}

Document.swift

import Foundation
import RealmSwift

class Document: Object {
@objc dynamic var title: String = ""
@objc dynamic var image: String = ""

//An inverse relationship with each Document having an associatedClient that is of the
//type Client and it comes from the property called "documents" from the Client forward relationship
var associatedClient = LinkingObjects(fromType: Client.self, property: "documents")
}

DocumentTableViewController.swift

import UIKit
import RealmSwift

class DocumentTableViewController: UITableViewController {

let realm = try! Realm()
var documents : Results<Document>?

//Data type optional document because it will be nil until it is set
var selectedClient : Client? {
    //Everything in the did set function will happen as soon as this variable is set with a value
    didSet {
        loadDocuments()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

//Mark - Model Manipulation Methods

func loadDocuments(){

    //Pulls all document objects out of our realm (persistent data)
    documents = selectedClient?.documents.sorted(byKeyPath: "title", ascending: true)

    tableView.reloadData()
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return documents?.count ?? 0
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "goToDocumentDetails", sender: self)
}

//Load the documents for the selected client
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let navigationController = segue.destination as? UINavigationController
    let viewController = navigationController?.topViewController as? DocumentDetailsViewController

    if let indexPath = tableView.indexPathForSelectedRow {
        viewController?.selectedDocument = documents?[indexPath.row]
    }
}

//MARK: - HELPER FUNCTIONS FOR TESTING

@IBAction func addTestDocument(_ sender: UIBarButtonItem) {

    if let currentClient = self.selectedClient {
        do {
            try self.realm.write {
                let newDocument = Document()
                newDocument.title = "TEST"
                currentClient.documents.append(newDocument)
                print(documents?.count ?? 0)
            }
        } catch {
            print("Error saving new document")
        }
    }

    loadDocuments()

}

}

DocumentDetailsViewController.swift

import UIKit
import RealmSwift

class DocumentDetailsViewController: UIViewController {

let realm = try! Realm()
//var documents : Results<Document>?

@IBOutlet weak var titleLabel: UILabel!

//Data type optional document because it will be nil until it is set
var selectedDocument : Document? {
    //Everything in the did set function will happen as soon as this variable is set with a value
    didSet {
        titleLabel.textColor = UIColor.blue
        titleLabel.text = selectedDocument?.title
    }
}

I expect that I'll be able to change values in my DocumentsDetailViewController programmatically after passing in the selected Document object but trying to change the value in any way produces a nil optional value exception.

Upvotes: 0

Views: 528

Answers (1)

vadian
vadian

Reputation: 285069

It's a common mistake / misunderstanding. It's not related to Realm at all.

At the moment you call

viewController?.selectedDocument = documents?[indexPath.row]

the outlets in the destination view controller are not connected yet, therefore titleLabel is nil in didSet.


A possible solution is to move the code in didSet to viewDidLoad

var selectedDocument : Document?

override func viewDidLoad() {
    super.viewDidLoad()
    titleLabel.textColor = UIColor.blue
    titleLabel.text = selectedDocument?.title
}

Upvotes: 1

Related Questions