【问题标题】:PFQuery findObjectsInBackgroundWithBlock not getting called?PFQuery findObjectsInBackgroundWithBlock 没有被调用?
【发布时间】:2016-05-01 12:21:38
【问题描述】:
let userQuery = PFUser.query()
userQuery!.whereKey("username", equalTo: userUsername)
userQuery!.findObjectsInBackgroundWithBlock({ (result:[PFObject]?, error:NSError?) in
    currentProfilePageUser = result![0] as! PFUser
 })

这是我的代码。我正在尝试仅使用用户名获取PFUser。但是,我在findObjectsInBackgroundWithBlock 中添加了断点,发现它没有被调用。因此,我无法接收我的数据。为什么findObjectsInBackgroundWithBlock 没有被调用?

let query = PFQuery(className: "Follow")
    query.whereKey("followFrom", equalTo: PFUser.currentUser()!.objectId!)
    query.whereKey("followingTo", equalTo: currentProfilePageUser)
    query.findObjectsInBackgroundWithBlock { (result:[PFObject]?, error:NSError?) in
        if result == nil{
            print("currentuser is not following this user")
            self.followButtonTitle.setTitle("Follow", forState: .Normal)
        }else{
            print("currentuser is follwing this user")
            self.followButtonTitle.setTitle("Unfollow", forState: .Normal)
        }
    }

由于一些非常奇怪的原因,第一个 findObjectsInBackgroundWithBlock 现在可以工作,但另一个没有被调用。这很奇怪。

【问题讨论】:

  • 它可能正在被调用,但由于它是“inBackground”,这意味着它是在后台线程上调用的。尝试测试错误并为它们添加打印语句。您应该会看到其中至少有一个被调用了。

标签: ios swift parse-platform swift2 pfquery


【解决方案1】:

试试这个,看看你的控制台是否打印出来:

let userQuery = PFUser.query()
userQuery!.whereKey("username", equalTo: userUsername)
userQuery!.findObjectsInBackgroundWithBlock({ (result:[PFObject]?, error:NSError?) in
    if(error == nil){
        print("Success!")
    } else {
        print("Failed with error \(error)")
    }
})

【讨论】:

  • 我不知道为什么,但这运行。我在这里放了一个断点,它到达了断点。我真的不明白为什么这个findObjectInBackgroundWithBlock 会运行,但有些却没有.....
  • 哇,现在 command+z 切换到旧代码,它运行完美。这太奇怪了。
  • 这个 findObjectsInBackgroundWithBlock 现在可以工作了,但我的另一个仍然不能工作。
  • 我也将我的第二个 findObjectsInBackgroundWithBlock 更改为这段代码,但它没有被调用,也没有运行。
  • 现在它都被调用了......天哪,这太奇怪了
【解决方案2】:

我在上面发布的第二个查询需要第一个查询的结果。出于某种原因,第二个查询首先运行,然后是第一个,无论我的代码顺序如何。我的解决方法是将第二个查询放在 viewDidAppear 中,将它放在 viewWillAppear 甚至都行不通。

【讨论】:

  • 这不是解决您问题的方法。您正在处理基本的并发问题。如果您有 2 个查询,其中第二个查询需要来自第一个的数据,您有两个选择:运行同步查询(错误)或确保在第一个查询完成之前不会触发您的第二个查询。您可以使用 Bolts 框架轻松完成此操作。检查此答案以获取更多信息:stackoverflow.com/questions/35161271/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 2019-03-31
  • 2012-12-16
相关资源
最近更新 更多