0

問題: ViewController の tableView cellForRowAtIndexPath メソッド内で以下の関数を呼び出すと、nil が返されますが、生成されたオブジェクトを、ViewController でグローバルに宣言した rankObject という名前の PFObject 変数に設定する必要があります。完了ハンドラーを含む多くのことを試しましたが、まだ行き詰まっています。このタスクをどのように達成しますか?

func getOrMakeRankRelations(sound: PFObject) {
    var rankRelation = sound.relationForKey("userRanks")
    let currentUser = PFUser.currentUser()!
    var relationQuery = rankRelation.query()
    relationQuery?.whereKey("user", equalTo: currentUser)
    relationQuery?.findObjectsInBackgroundWithBlock { (theObject: [AnyObject]?, error: NSError?) -> Void in
        if (theObject?.isEmpty == true) {
            println("Rank object is empty")

            //Make Ranking PFObject
            var ranking = PFObject(className: "Ranking")
            ranking["user"] = PFUser.currentUser()!
            ranking["Rank"] = 0

            //Upload ranking to Parse
            ranking.saveInBackgroundWithBlock { (saved: Bool, error: NSError?) -> Void in
                if saved {
                    println("Saved ranking")

                    //Add relation between sound and ranking
                    rankRelation.addObject(ranking)
                    sound.saveInBackgroundWithBlock { (saved: Bool, error: NSError?) -> Void in
                        if saved {
                            println("Saved relation")
                            var theObject = (theObject![0] as? PFObject)!
                            self.rankObject = theObject //////////NOTE:  the variable saves while in this closure /////////////
                        } else {
                            println("Relation not saved :(")
                        }
                    }
                } else {
                    println(error)
                }
            }
        } else {
            var theObject = (theObject![0] as? PFObject)!
            self.rankObject = theObject  //////////NOTE:  the variable also saves while in this closure /////////////
        }
    }
}

tableView で関数を呼び出す方法は次のとおりです。

getOrMakeRankRelation(sound)
4

1 に答える 1