ios - Parse 在第一次查询尝试时不返回
问题描述
我有一个段控件,它确定加载到表视图中的内容。我在 viewDidLoad 中加载索引 0 的表,它工作正常。通过情节提要“选择”段控件以索引 0。当索引切换到索引 1 时,我收到警报消息,我设置了“没有拥有的项目”,它让我停留在索引 0。当我第二次单击索引 1 时,它会找到项目并加载我想要的表索引 1. 为什么 getOwnedCosmetics 的查询在第一次将段更改为索引 1 时没有返回任何内容?我打印化妆品IdOwned 并返回 [ ]。第二次用对象填充数组。
import UIKit
import Parse
class SkinsTableViewController: UITableViewController {
var cosmeticTracker = "Skins"
var cosmeticName = [String]()
var cosmeticImage = [PFFile]()
var cosmeticRarity = [String]()
var cosmeticId = [String]()
var owned = "Owned"
var cosmeticIdOwned = [String]()
@IBOutlet var cosmeticTable: UITableView!
@IBOutlet weak var seg: UISegmentedControl!
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
switch seg.selectedSegmentIndex {
case 0:
getAllCosmetics(cosmetic: cosmeticTracker)
case 1:
getOwnedCosmetics(cosmetic: cosmeticTracker)
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
getAllCosmetics(cosmetic: cosmeticTracker)
}
@objc func getOwnedCosmetics (cosmetic : String){
if PFUser.current() != nil {
let query = PFQuery(className: (cosmetic + owned))
query.whereKey("theUser", equalTo: PFUser.current()?.objectId as Any)
query.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let objects = objects {
self.cosmeticIdOwned.removeAll()
for object in objects {
self.cosmeticIdOwned.append(object["theCosmetic"] as! String)
}
}
})
if cosmeticIdOwned.count > 0 {
getOwnedCosmeticsQuery()
}
else {
self.displayAlert(title: "No items owned", message: "Add \(cosmeticTracker) to your colection")
seg.selectedSegmentIndex = 0
}
}
else {
self.displayAlert(title: "Unable to retrieve data", message: "Please sign in")
seg.selectedSegmentIndex = 0
}
}
func getOwnedCosmeticsQuery (){
let queryOwned = PFQuery(className: cosmeticTracker)
queryOwned.whereKey("objectId", containedIn: cosmeticIdOwned as [AnyObject])
queryOwned.order(byAscending: "Rarity")
queryOwned.addAscendingOrder("Name")
queryOwned.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let objects = objects {
self.cosmeticName.removeAll()
self.cosmeticImage.removeAll()
self.cosmeticId.removeAll()
self.cosmeticRarity.removeAll()
for object in objects {
self.cosmeticName.append(object["Name"] as! String)
self.cosmeticImage.append(object["Image"] as! PFFile)
self.cosmeticId.append(object.objectId as String!)
self.cosmeticRarity.append(object["Rarity"] as! String)
self.cosmeticTable.reloadData()
if self.cosmeticIdOwned.count > 99 {
self.getOwnedCosmeticsQueryTwo()
}
}
}
})
}
}
如果我在 viewDidLoad 中填充了一个项目,我想说明一下。即使它实际上不是“拥有”的,它也会让它 getOwnedCosmeticsQuery 并找到该项目。当我第二次单击 getOwnedCosmetics 段时,它会使用从 getOwnedCosmetics 中找到的正确对象运行 getOwnedCosmeticsQuery。
解决方案
推荐阅读
- c# - 使用 YamlDotNet 将字符串嵌套到类反序列化
- java - 无法通过 Python 的 Spring Boot REST API POST 调用上传文件
- typescript - 有什么方法可以缩小T吗?
- linux - opencv 在 raspi pi 4 上包含和 lib 路径
- soap - 如何使用 MIT App Inventor 与 SOAP API 进行通信
- amazon-web-services - 如何使用 Amazon EventBridge 发送预定的自定义消息
- php - MongoDB 拒绝 Laravel 8
- javascript - 检测电子邮件是否来自多个域的更好方法
- android - Flutter:“未找到方法的实现” - 用于缓存的颤振引擎
- node.js - 如何使用带有 as400 smtp 服务的 nodemailer 发送电子邮件