Function and reset in Parse.com



  • There's a function to verify if the record is in the database. parse.com:

    func checkObject (login:String) {
        let object = PFQuery(className: "MapObject")
        object.whereKey("User", equalTo: login)
        object.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if objects! == [] {
                var typeofuser = false
                print(typeofuser)
            } else {
                var typeofuser = true
                print(typeofuser)
            }
        }
    }
    

    Her call is tied to the button and she works right. But I want to change it so she can come back. typeofuser, i.e. true or false♪ Attempts to add -> Bool and return typeofuser Makes a mistake. What's the right syn


  • QA Engineer

    The problem is that the method findObjectsInBackgroundWithBlock Asynchronous, it is therefore not known how long this function will take and, more importantly, the annex will not wait until it is over and move immediately to the next line of the code. I propose to change that way:

    func checkObject (login:String, completionHandler : (typeofuser : Bool) -> ()) {
      let object = PFQuery(className: "MapObject")
      object.whereKey("User", equalTo: login)
      object.findObjectsInBackgroundWithBlock { (objects, error) in
        if objects! == [] {
          completionHandler(typeofuser : false)
        } else {
          completionHandler(typeofuser : true)
        }
      }
    }
    

    You can use that.

    var checkResult : Bool?
    

    checkObject("login") { typeofuser in
    checkResult = typeofuser
    }

    Look that checkResult type Optional Because at the time you might need it, it's not the fact that the database request will be processed.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2