2

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?

Mo Code
  • 117
  • 1
  • 14
  • See: http://stackoverflow.com/questions/24726723/stop-pause-swift-app-for-period-of-time and http://stackoverflow.com/questions/27517632/how-to-create-a-delay-in-swift – WBT Apr 30 '16 at 04:18
  • Is a sleep/pause the best method of handling my problem or is there a better way to know when that thread finishes? – Mo Code Apr 30 '16 at 04:22
  • Callbacks are better than sleep, but the links are primarily based on how you've titled your question. – WBT Apr 30 '16 at 14:32

1 Answers1

0

You need to execute the code that accesses the titles and urls in an asynchronous callback.

Use:

var db = VideoDatabaseHandler()
var video_titles = [String]()
var video_urls = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    //Load video list
    db.loadPFObject("TutorialVids") { video_list in
        video_titles = video_list["titles"] as! [String]
        video_urls = video_list["urls"] as! [String]
        self.tableView.reloadData()
    }
}

and

//This function loads a PFObject from the database
func loadPFObject(category: String, onSuccess: (PFObject -> Void)){
    let query = PFQuery(className: category)

    if(category == "TutorialVids"){
        query.getObjectInBackgroundWithId("vpWEueKv5O"){
            (tutVideos: PFObject?, error: NSError?) -> Void in

            //successfully loaded video lists
            if(error == nil && tutVideos != nil) {
                onSuccess(tutVideos!)
            }

                //Error loading video lists
            else{
                print(error)
            }
        }
    }
}
paulvs
  • 11,963
  • 3
  • 41
  • 66