gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:09:15 28 4
gpt4 key购买 nike

我正在尝试弄清楚如何在运行 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)
})

}

问题是加载屏幕在查询结束前被移除了。我该怎么做才能让它只在查询完成后停止?请帮忙。提前致谢。

最佳答案

你的 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)

关于ios - 在 Swift 中运行 API 查询时如何显示加载屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35964520/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com