简体   繁体   English

parse.com - 在iOS中获取对象延迟

[英]parse.com - Get Object delay in iOS

I would like to get an object and print it. 我想得到一个对象并打印出来。 So I have created a function that will deal with getting the object and returning it back. 所以我创建了一个函数来处理获取对象并将其返回。

The problem is, the object is printed too early. 问题是,对象打印得太早。

This is my function: 这是我的功能:

func getParseObject(){
  println("i'm inside the function")
  var query = PFQuery(className: "testClass")
  query.getObjectInBackgroundWithId("OWXnWkL") {(tempObject: PFObject?, error: NSError?) -> Void in
    if error == nil && tempObject != nil {
      println("object retrieved with success")
    } else {
      //error
    }
  }
  return object
}

and this is the call: 这就是电话:

var data = getParseObject()
println("data is: \(data)")

And this is the output of the console: 这是控制台的输出:

i'm inside the function 我在功能里面

data is: 数据是:

object retrieved with success 对象成功检索

What is the best solution to deal with this problem? 处理这个问题的最佳解决方案是什么?

Many thanks! 非常感谢!

your problem is you're doing an asynchronous request, the request is executed in the background. 你的问题是你在做异步请求,请求是在后台执行的。 Therefore, you return the object before having recovered the data on the server. 因此,您在恢复服务器上的数据之前返回对象。

If you want to display the value returned by the function of the need to synchronously: 如果要显示需要同步的函数返回的值:

func getParseObject() -> PFObject {
    var object : PFObject?
    let query = PFQuery(className: "testClass")
    let result =  query.getObjectWithId("OWXnWkL")
    if result != nil {
        object = result
    }
        return object!
}

else you must to work with handlers : 否则你必须与处理程序一起工作:

var query = PFQuery(className: "testClass")
  query.getObjectInBackgroundWithId("OWXnWkL") {(tempObject: PFObject?, error: NSError?) -> Void in
    if error == nil && tempObject != nil {
      println("object retrieved with success")
      // Do something with the object!
    } else {
      //error
    }

I hope I have helped you 我希望我能帮到你

Ysee } Ysee}

You need to return your object value within the callback of the asynchronous request like this: 您需要在异步请求的回调中返回您的对象值如下所示:

func getParseObject(){
  println("i'm inside the function")
  var query = PFQuery(className: "testClass")
  query.getObjectInBackgroundWithId("OWXnWkL") {(tempObject: PFObject?, error: NSError?) -> Void in
    if error == nil && tempObject != nil {
      println("object retrieved with success")
    } else {
      //error
    }

    return object // <-- Return your value here
  } // <-- Asynchronous request ends here

}

The code from the closure from the getObjectInBackgroundWithId method is asynchronous and takes more time to be completed. 来自getObjectInBackgroundWithId方法的闭包中的代码是异步的,需要更多时间才能完成。 So the code that is written after the closure ends is going to run first. 所以在闭包结束后编写的代码将首先运行。

func getParseObject(){
  println("i'm inside the function")
  var query = PFQuery(className: "testClass")
  query.getObjectInBackgroundWithId("OWXnWkL") {(tempObject: PFObject?, error: NSError?) -> Void in
    if error == nil && tempObject != nil {
      println("object retrieved with success")
//THIS RUNS SECOND::::::::
        } else {
          //error
        }
      }
      return object
    }
    var data = getParseObject()
    println("data is: \(data)") //:::::THIS RUNS FIRST::::::

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM