带有请求完成处理程序的数据并不总是被调用

data with request completionhandler not always called

我正在制作一个应用程序,您可以在其中使用 http://www.omdbapi.com/ 的 API 搜索电影。

我遇到的问题是 dataTaskWithRequest 的完成处理程序。如果您单击其中一个 collectionView 单元格,您将转到所选电影的 detailView。然而,它并不总是有效。我收到一条错误消息:展开时意外发现 nil。那是因为它没有进入 dataTaskWithRequest 的完成处理程序,而是直接进入 detailVC 并尝试在标题标签、流派标签等中传递数据,但没有数据。

我希望你们知道问题是什么,因为我已经尝试过,但我没有看到问题是什么。

或者,这个问题是不是因为之前的事情导致的?因为首先我使用 "by search" 而不是 "by ID" 从 http://www.omdbapi.com/ 检索数据。然后我从那里检索 ID,并从该 ID 检索我的 detailVC 的数据。

这是我的代码:

 // Go to detail view of selected movie
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let selectedMovieId = self.IDs[indexPath.row]

    chosenMovieId = selectedMovieId

    self.performSegueWithIdentifier("showDetail", sender: self)
}

// Preparations before going to the detail view of selected movie
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showDetail" {

        _ = self.movieInfoFrom(searchMovie: chosenMovieId, segue: segue)
    }
}


func movieInfoFrom(searchMovie movieId: String, segue: UIStoryboardSegue) {

    let movieUrlString = "http://www.omdbapi.com/?i=\(movieId)&y=&plot=full&r=json"
    let url = NSURL(string: movieUrlString)
    print(movieUrlString)

    let urlRequest = NSURLRequest(URL: url!)
    let urlSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

    let urlTask = urlSession.dataTaskWithRequest(urlRequest) { (data, response, error) in

        if error == nil {

            // Convert data to JSON
            let swiftyJSON = JSON(data: data!)

            let title = swiftyJSON["Title"].string!
            let runTime = swiftyJSON["Runtime"].string!
            let genre = swiftyJSON["Genre"].string!
            let plot = swiftyJSON["Plot"].string!
            let rating = swiftyJSON["imdbRating"].string!
            let year = swiftyJSON["Year"].string!
            let poster = swiftyJSON["Poster"].string

            self.infoResult = ["\(title)", "\(runTime)", "\(genre)", "\(plot)", "\(rating)", "\(year)"]
            print("\(self.infoResult)")

            let destinationVC = segue.destinationViewController as! MovieDetailController

            destinationVC.movieDetails = self.infoResult
            destinationVC.moviePoster = poster
        }
    }

    urlTask.resume()
}

我试图修复你的代码并用一些评论进行解释:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let selectedMovieId = self.IDs[indexPath.row]

    chosenMovieId = selectedMovieId

    self.movieInfoFrom(searchMovie: chosenMovieId)
}

// Preparations before going to the detail view of selected movie
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showDetail" {

        let destinationVC = segue.destinationViewController as! MovieDetailController

            destinationVC.movieDetails = self.infoResult
            destinationVC.moviePoster = poster
    }
}


func movieInfoFrom(searchMovie movieId: String) {

    let movieUrlString = "http://www.omdbapi.com/?i=\(movieId)&y=&plot=full&r=json"
    let url = NSURL(string: movieUrlString)
    print(movieUrlString)

    let urlRequest = NSURLRequest(URL: url!)
    let urlSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

    // This is asynchronously, you can put a loading here 
    let urlTask = urlSession.dataTaskWithRequest(urlRequest) { (data, response, error) in
        // Got response, stop loading here
        if error == nil {

            // Convert data to JSON
            let swiftyJSON = JSON(data: data!)

            let title = swiftyJSON["Title"].string!
            let runTime = swiftyJSON["Runtime"].string!
            let genre = swiftyJSON["Genre"].string!
            let plot = swiftyJSON["Plot"].string!
            let rating = swiftyJSON["imdbRating"].string!
            let year = swiftyJSON["Year"].string!

            // You can save the poster as local variable
            let poster = swiftyJSON["Poster"].string

            self.infoResult = ["\(title)", "\(runTime)", "\(genre)", "\(plot)", "\(rating)", "\(year)"]
            print("\(self.infoResult)")

            // This should be call on main thread
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier("showDetail", sender: self)
            }


        }
    }

    urlTask.resume()
}

试试这段代码,使用安全的选项

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    chosenMovieId = self.IDs[indexPath.row]
    movieInfoFrom(searchMovie: chosenMovieId)

}

func movieInfoFrom(searchMovie movieId: String) {

    let movieUrlString = "http://www.omdbapi.com/?i=\(movieId)&y=&plot=full&r=json"
    let url = NSURL(string: movieUrlString)
    print(movieUrlString)

    let urlRequest = NSURLRequest(URL: url!)
    let urlSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

    let urlTask = urlSession.dataTaskWithRequest(urlRequest) { (data, response, error) in

        if error == nil && data != nil {

            // Convert data to JSON
            let swiftyJSON = JSON(data: data!)

            let title = swiftyJSON["Title"].string ?? ""
            let runTime = swiftyJSON["Runtime"].string ?? ""
            let genre = swiftyJSON["Genre"].string ?? ""
            let plot = swiftyJSON["Plot"].string ?? ""
            let rating = swiftyJSON["imdbRating"].string ?? ""
            let year = swiftyJSON["Year"].string ?? ""
            let poster = swiftyJSON["Poster"].string

            self.infoResult = ["\(title)", "\(runTime)", "\(genre)", "\(plot)", "\(rating)", "\(year)"]
            print("\(self.infoResult)")

            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier("showDetail", sender: poster)
            }
        }
    }
    urlTask.resume()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showDetail", let destinationVC = segue.destinationViewController as? MovieDetailController {

        destinationVC.movieDetails = self.infoResult
        destinationVC.moviePoster = sender as? String
    }
}