阵列重新调整PFQuery外空数组解析。 [英] Array retuning a blank array outside of PFQuery with Parse.

查看:132
本文介绍了阵列重新调整PFQuery外空数组解析。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阵列返回一个空阵列时PFQuery之外。出于某种原因,该项目不被编译的时候传递给数组。

 类DriverViewController:UIViewController的{
VAR placesArr:数组<&地点GT; = []
    覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()
    self.window =的UIWindow(帧:UIScreen.mainScreen()边界)
    VAR的查询= PFQuery(的className:地方)
    query.whereKey(用户名,equalTo:email@email.com)
    query.findObjectsInBackgroundWithBlock {
        (?对象:[AnyObject]?错误:NSError) - GT;在无效        如果错误== {为零
            的println(成功检索\\(对象!.Count之间)的分数。)            如果让对象=对象作为? [PFObject] {
                在对象的对象{
             令x =广场(aIdent:(对象[标识]为INT),aName:(对象[名称]为String),aAddress!(对象[originAddress]为String),aCity:( [对象originCity]为String),aCategoryName!(对象[catName]为String),阿拉特!(对象[阿拉特]为String),aLng!(对象[aLng]作为!串))                    self.placesArr.append(X)
                    的println(placesArr)// ****它在这里工作并打印数组****
                }
            }
        }其他{
            //登录失败的细节
            的println(错误!!!\\(错误)\\(错误.userInfo))
        }
    }
 的println(placesArr)// ****但在这里它返回一个空数组,这是我需要它返回一个数组****


解决方案

这与线程一个很普遍的误解,问题是什么秩序活动运行:

  //第一次运行
query.findObjectsInBackgroundWithBlock {
    (?对象:[AnyObject]?错误:NSError) - GT;在无效
    //运行第3
}
//第二次运行
的println(placesArr)

程序的执行,当你调用不会暂停 findObjectsInBackground ,它找到对象: inBackground 这意味着一个网络请求的繁重被分配到一个不同的队列,使得用户仍然可以与屏幕交互。一个简单的方法来做到这一点是做:

  VAR placesArray:[地方] = [] {
    didSet {
        //请在需要等待的地方阵列这里的任何执行。
    }
}

您还可以触发解析响应块中的后续行动,我只是个人认为执行行为依赖于一个属性更新在 didSet 正在执行要控制的好方法流程。

Array returning a blank array when outside of the PFQuery. For some reason, the items are not being passed to the array when compiled.

class DriverViewController: UIViewController {
var placesArr : Array<Place> = []


    override func viewDidLoad() {
    super.viewDidLoad()
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)


    var query = PFQuery(className:"places")
    query.whereKey("username", equalTo:"email@email.com")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects!.count) scores.")

            if let objects = objects as? [PFObject] {
                for object in objects {
             let x = Place(aIdent: (object["Ident"] as! Int), aName: (object["name"] as! String), aAddress: (object["originAddress"] as! String), aCity: (object["originCity"] as! String), aCategoryName: (object["catName"] as! String), aLat: (object["aLat"] as! String), aLng: (object["aLng"] as! String))

                    self.placesArr.append(x)
                    println(placesArr) //****It works here and prints an array****
                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
 println(placesArr) //****But here it returns a blank array and this is where I need it to return an array****

解决方案

This a very common misunderstanding relating to threading, the issue is what order events run:

// Runs 1st
query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) -> Void in
    // Runs 3rd
}
// Runs 2nd
println(placesArr)

The execution of the program doesn't halt when you call findObjectsInBackground, it finds objects: inBackground which means the heavy lifting of a network request is dispatched to a different queue so that the user can still interact with the screen. A simple way to do this would be to do:

var placesArray: [Place] = [] {
    didSet {
        // Do any execution that needs to wait for places array here.
    }
}

You can also trigger subsequent actions within the parse response block, I just personally find executing behavior dependent on a property update being executed in didSet to be a nice way to control flow.

这篇关于阵列重新调整PFQuery外空数组解析。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆