gpt4 book ai didi

ios - HandleWatchKitExtensionRequest 中的嵌套 PFQuery

转载 作者:搜寻专家 更新时间:2023-11-01 07:32:31 24 4
gpt4 key购买 nike

我正在尝试将 2 个 PFQuery 嵌套在 HandleWatchKitExtensionRequest 中,以便我可以在回复中将数据传回我的 Watch Extension。在下面的代码中,第一个查询(对于匹配给定用户名的 PFUser)返回,但我无法让第二个(对于用户人员列表)返回。在同一个 block 中进行多个查询时,我缺少一些限制吗?在这个长时间运行的请求返回之前,我的后台任务是否超时?

*我编辑了我的代码,使 nameList 不是可选的,但仍然没有返回值。虽然同一个 block (嵌套的 findObjectsInBackGroundBlocks)在我的 IOS 应用程序中执行并完美返回。

延长后台任务的延迟时间没有帮助。

 func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {


var dictionary = userInfo! as NSDictionary


if let currentUserName: AnyObject = dictionary.objectForKey("currentUserName") {
if let currentUserNameAsString = currentUserName as? String {


// Bogus task for keeping app going
var bogusTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
}
// End bogus task after 2 seconds
delay(2.0, closure: { () -> () in
UIApplication.sharedApplication().endBackgroundTask(bogusTask)
})

// look up current user logged in on phone from name passed from Watch
let query = PFUser.query()
query!.whereKey("username", equalTo: currentUserNameAsString)
query!.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if let user = objects?.first as? PFUser {

var testName: String?
var nameList: String = []
let query = PFQuery(className: Person.parseClassName())
query.whereKey("user", equalTo: user)

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let people = objects as? [Person] {
for person in people {
if let name = person.name {
nameList.append(name)
testName = nameList.first
}
}
}
testName = ("test")
}






let testDict = ["success" : testName!]
reply(testDict)

UIApplication.sharedApplication().endBackgroundTask(bogusTask) // End the bogusTask in case the work was faster than 2 seconds
}
}


}

}




}

// Utility function for delay
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(),
closure
)
}

最佳答案

看起来您在嵌套查询之外调用“reply(testDict)”。

由于查询是异步的,“reply(testDict)”可能在嵌套查询完成执行之前被调用。

关于ios - HandleWatchKitExtensionRequest 中的嵌套 PFQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com