阻止for-in循环

时间:2015-09-16 12:37:30

标签: facebook swift for-in-loop

我有一个for-in循环和一些代码。我希望在循环完成后执行该段代码。我怎样才能做到这一点?它看起来像这样:

            // facebook friends
            let fbRequest = FBSDKGraphRequest(graphPath:"me/friends", parameters: nil);
            fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
                if error == nil {
                    let data : NSArray = result.objectForKey("data") as! NSArray
                    for item in data {
                        print(item)
                        let queryIn1 = PFQuery(className:"Friendship")
                        queryIn1.whereKey("toUser", equalTo: PFUser.currentUser()!.username!)
                        queryIn1.whereKey("fromUser", equalTo: item["name"] as! String )
                        let queryOut = PFQuery(className:"Friendship")
                        queryOut.whereKey("fromUser", equalTo: PFUser.currentUser()!.username!)
                        queryOut.whereKey("toUser", equalTo: item["name"] as! String )
                        let query = PFQuery.orQueryWithSubqueries([queryIn1, queryOut])
                        query.findObjectsInBackgroundWithBlock {
                            (objects: [AnyObject]?, error: NSError?) -> Void in
                            if error == nil {
                                if objects!.count == 0 {
                                    let request = PFObject(className: "Friendship")
                                    request["fromUser"] = PFUser.currentUser()!.username!
                                    request["toUser"] = item["name"] as! String
                                    request["status"] = "approved"
                                    request.saveInBackgroundWithBlock{(success, error) -> Void in
                                        print(item["name"] as! String)
                                        globalClass.myFriends.append(item["name"] as! String)
                                        globalClass.myFriends = globalClass.myFriends.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
                                    }
                                } else {
                                }
                            }
                        }
                    }
                    self.defaults.setObject(true, forKey: "FB")
                    globalClass.facebook = true
                    self.performSegueWithIdentifier("firstFriends", sender: self)
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    self.spinningView.hidden = true
                } else {
                }
            }

编辑:这是整个代码。实际上,在创建所有对象之前调用segue。

gdal_translate your_HDF_file -sds output.tif

1 个答案:

答案 0 :(得分:0)

通过添加如下部分来修复它:

                                    let request = PFObject(className: "Friendship")
                                    request["fromUser"] = PFUser.currentUser()!.username!
                                    request["toUser"] = item["name"] as! String
                                    request["status"] = "approved"
                                    request.saveInBackgroundWithBlock{(success, error) -> Void in
                                        print(item["name"] as! String)
                                        globalClass.myFriends.append(item["name"] as! String)
                                        globalClass.myFriends = globalClass.myFriends.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
                                        if globalClass.myFriends.count == data.count {
                                            print(globalClass.myFriends.count)
                                            print(data.count)
                                            self.defaults.setObject(true, forKey: "FB")
                                            globalClass.facebook = true
                                            self.performSegueWithIdentifier("firstFriends", sender: self)
                                            UIApplication.sharedApplication().endIgnoringInteractionEvents()
                                            self.spinningView.hidden = true
                                        }
                                    }