TechDude729
TechDude729

Reputation: 13

iOS Swift w/Firebase Integration - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

So I'm fairly new to iOS programming (but used-to enough to implement Firebase) and I was essentially making an app with view controllers as such: VC Screenshot

I have a segue from the Events button on the middle screen going to the last one, and when I try it out in a simulator it works. However, on the third screen, my app (or, rather, the VC) fetches data from a Firebase database like so:

let ref = FIRDatabase.database().reference().child("events")

    ref.observeSingleEvent(of: .value, with: { snapshot in
        for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
            if rest.key != "upcoming" {
                self.data.append(EventData(eventName: rest.childSnapshot(forPath: "name").value as! String, eventDate: rest.childSnapshot(forPath: "date").value as! String, eventInfo: rest.childSnapshot(forPath: "desc").value as! String, eventKey: rest.key, eventUECode: rest.childSnapshot(forPath: "ue_code").value as! String))

                print(rest.childSnapshot(forPath: "name"))
            }
        }
        super.loadView()
    })

On clicking Events in a simulator, I see the app transition to the third screen and see the content resulting from the above pull, but the content disappears when the transition completes. Then, if I go back, my second screen appears blank, and clicking on Events again caused the exception mentioned in the title to be thrown.

I have tried to disable animation on the segue and it works, but I can't figure out what's causing the error with animations, and how to fix it?

EDIT I forgot to mention, the code is being run in the loadView() function, as I'm populating a UITableView.

Upvotes: 0

Views: 181

Answers (1)

Son of a Beach
Son of a Beach

Reputation: 1779

Delete the line: super.loadView()

The documentation for this method says, "You should never call this method directly". It will be called internally by the API when required (eg, first time self.view is called). The doco also says that if you override loadView, "Your custom implementation of this method should not call super." See: https://developer.apple.com/reference/uikit/uiviewcontroller/1621454-loadview

Is there some reason you're trying to call it directly? If you really need it to be called at some time other than when it would normally get called automatically, you can call self.view instead. That should result in a correct internal call to loadView.

Upvotes: 1

Related Questions