如何在Swift中运行API查询时显示加载屏幕?

时间:2016-03-12 23:20:58

标签: ios swift user-interface google-places-api

我试图弄清楚如何在运行API查询和保存数据时显示加载屏幕。这是在SWIFT,我正在使用Alamofire。我的最新尝试涉及向API搜索方法添加完成处理程序......

func search(keyword: String, location: String, radius: String, completion: () -> ()) {

    urlString = self.baseURL + self.KEY + location + radius + keyword

    AlamoManager.request(.GET, urlString).responseJSON { response in

        if let newJSON = response.result.value, let results = newJSON["results"] {
            let newPlaces = results!.valueForKey("name") as! [AnyObject]

            for newPlace in newPlaces {
                self.cd.saveData(newPlace as! String)
            }

            print("\nSAVED 1ST BATCH...\n")

            if let token = newJSON["next_page_token"] as? String {
                let nextPageURL = self.baseURL + self.KEY + "&pagetoken=\(token as! String)"
                sleep(3)

                self.AlamoManager.request(.GET, nextPageURL).responseJSON { response in
                    if let newJSON2 = response.result.value , let results = newJSON2["results"] {
                        let newPlaces2 = results!.valueForKey("name") as! [AnyObject]

                        for newPlace in newPlaces2 {
                            self.cd.saveData(newPlace as! String)
                        }
                        print("\nSAVED 2ND BATCH...\n")

                        if let token = newJSON2["next_page_token"] as? String {
                            let lastPageURL = self.baseURL + self.KEY + "&pagetoken=\(token as! String)"
                            sleep(3)

                            self.AlamoManager.request(.GET, lastPageURL).responseJSON { response in
                                if let newJSON2 = response.result.value, let results = newJSON2["results"] {

                                    var newPlaces3 = results?.valueForKey("name") as! [AnyObject]

                                    for newPlace in newPlaces3 {
                                        self.cd.saveData(newPlace as! String)
                                    }
                                    print("\nSAVED 3RD BATCH...\n")
                                } else {
                                    print("Didn't get the 3rd JSON.")
                                }
                            }
                        } else {
                            print("NO MORE PAGES AVAILABLE")
                        }
                    } else {
                        print("Didn't get the second json")
                    }
                }
            }
        }

        completion()
    }
}

...然后在viewDidLoad中调用加载动画并在完成调用中停止它....

override func viewDidLoad() {
    super.viewDidLoad()

    self.loadingOverlayView.showLoadingOverlay(self.view)

    self.gp.search(self.keyword, location: location, radiusInput: "5000", completion: { () -> () in
        self.loadingOverlayView.hideLoadingOverlay(self.view)
    })

}

问题是在查询结束之前删除了加载屏幕。如何才能使它只在查询完成后停止?请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您的completion()来电应该是您打印"\nSAVED 3RD BATCH...\n"

的地方
func search(keyword: String, location: String, radius: String, completion: () -> ()) {

    urlString = self.baseURL + self.KEY + location + radius + keyword

    AlamoManager.request(.GET, urlString).responseJSON { response in

        if let newJSON = response.result.value, let results = newJSON["results"] {
            let newPlaces = results!.valueForKey("name") as! [AnyObject]

            for newPlace in newPlaces {
                self.cd.saveData(newPlace as! String)
            }

            print("\nSAVED 1ST BATCH...\n")

            if let token = newJSON["next_page_token"] as? String {
                let nextPageURL = self.baseURL + self.KEY + "&pagetoken=\(token as! String)"
                sleep(3)

                self.AlamoManager.request(.GET, nextPageURL).responseJSON { response in
                    if let newJSON2 = response.result.value , let results = newJSON2["results"] {
                        let newPlaces2 = results!.valueForKey("name") as! [AnyObject]

                        for newPlace in newPlaces2 {
                            self.cd.saveData(newPlace as! String)
                        }
                        print("\nSAVED 2ND BATCH...\n")

                        if let token = newJSON2["next_page_token"] as? String {
                            let lastPageURL = self.baseURL + self.KEY + "&pagetoken=\(token as! String)"
                            sleep(3)

                            self.AlamoManager.request(.GET, lastPageURL).responseJSON { response in
                                if let newJSON2 = response.result.value, let results = newJSON2["results"] {

                                    var newPlaces3 = results?.valueForKey("name") as! [AnyObject]

                                    for newPlace in newPlaces3 {
                                        self.cd.saveData(newPlace as! String)
                                    }
                                    print("\nSAVED 3RD BATCH...\n")
                                    completion() /// <------ HERE
                                } else {
                                    print("Didn't get the 3rd JSON.")
                                    completion() /// <------ HERE
                                }
                            }
                        } else {
                            print("NO MORE PAGES AVAILABLE")
                            completion() /// <------ HERE
                        }
                    } else {
                        print("Didn't get the second json")
                        completion() /// <------ HERE
                    }
                }
            }
        } else {
            completion() /// <------ HERE
        }

    }
}

另外,为什么要添加sleep(3)