I've got a VideoViewController class that calls my database class and does a loadPFObject and that returns a PFObject.
var db = VideoDatabaseHandler()
var video_titles = [String]()
var video_urls = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//Load video list
let video_list = db.loadPFObject("TutorialVids")
video_titles = video_list["titles"] as! [String]
video_urls = video_list["urls"] as! [String]
self.tableView.reloadData()
}
Then in my database class here is the function to return the PFObject
//This function loads a PFObject from the database
func loadPFObject(category: String) -> PFObject {
let query = PFQuery(className: category)
var db_obj = PFObject(className: category)
if(category == "TutorialVids"){
query.getObjectInBackgroundWithId("vpWEueKv5O"){
(tutVideos: PFObject?, error: NSError?) -> Void in
//successfully loaded video lists
if(error == nil && tutVideos != nil) {
db_obj = tutVideos!
}
//Error loading video lists
else{
print(error)
}
}
}
print(db_obj)
return db_obj
}
I keep getting an error in my first part of code I posted on the line:
video_titles = video_list["titles"] as! [String]
It says unexpectedly found nil while unwrapping an Optional value.
I believe that the loadPFObject since it's in a different class runs on a different thread and isn't finished loading the PFObject by the time it goes to unwrap that string value.
What would I do to fix this? Should I add a timer pause in the main thread or is my assumption about the threads completely wrong and the error is something entirely different?