第一次查询时解析不返回

时间:2018-07-21 23:44:45

标签: ios swift uitableview parse-platform uisegmentedcontrol

我有一个段控件,该控件确定将哪些内容加载到表视图中。我在viewDidLoad中加载索引为0的表,并且工作正常。通过情节提要“选择”分段控件以将其索引为0。当索引切换到索引1时,我收到警报消息,设置为“没有拥有的物品”,它停留在索引0上。当我第二次单击索引1时,它将查找项目并加载我想要的表index 1.为什么将段第一次更改为索引1时对getOwnedCosmetics的查询不返回任何内容?我打印了CosmeticIdOwned,它返回[]。第二次用对象填充数组。

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中的项目填充了CosmeticIdOwned。即使它实际上不是“拥有的”,也可以使其进入getOwnedCosmeticsQuery并找到该项目。当我第二次单击getOwnedCosmetics的细分时,它将使用从getOwnedCosmetics找到的正确对象运行getOwnedCosmeticsQuery。

0 个答案:

没有答案