首页 > 解决方案 > 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。

标签: iosswiftuitableviewparse-platformuisegmentedcontrol

解决方案


推荐阅读