我正在使用swift和parse构建一个应用程序。我想实现一个"下拉"刷新。只要我的应用程序查询服务器,这就会触发一个小动画(喜欢类似)。要查询服务器,我有一个具有执行两个背景块的函数的类:
class serverCom: NSObject, NSFetchedResultsControllerDelegate {
static var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
static var managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
static var mainUser = NSUserDefaults.standardUserDefaults()
static func letsGo() {
let userKindOfShit = [31, 32, 33, 41, 42, 43]
let searchAndDestroy = [31, 41, 42, 43]
let searchAndAppend = [31, 32, 33]
let notifKindOfShit = [11, 12, 13, 14, 21, 22, 23, 24]
let theBigAQuery: PFQuery = PFQuery(className: "Notification")
let theBigBQuery: PFQuery = PFQuery(className: "Notification")
theBigAQuery.whereKey("nameA", equalTo: (PFUser.currentUser()?.username)!)
theBigBQuery.whereKey("nameB", equalTo: (PFUser.currentUser()?.username)!)
theBigAQuery.whereKey("seenA", equalTo: false)
theBigBQuery.whereKey("seenB", equalTo: false)
theBigAQuery.findObjectsInBackgroundWithBlock { (theNoots: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if theNoots!.count == 0 {
} else {
for noot in theNoots! {
theBigAQuery.getObjectInBackgroundWithId(noot.objectId!) { (thatNoot: PFObject?, error: NSError?) -> Void in
if error == nil {
thatNoot!.setValue(true, forKey: "seenA")
thatNoot!.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
}
})
let typeOfNoot: Int = Int(thatNoot!.objectForKey("type") as! NSNumber)
let idOfTheGuy: String = thatNoot!.objectForKey("idOfB") as! String!
if userKindOfShit.contains(typeOfNoot) {
searchAndDestroying(idOfTheGuy)
if searchAndAppend.contains(typeOfNoot) {
searchAndAppending(idOfTheGuy, type: typeOfNoot)
}
} else if notifKindOfShit.contains(typeOfNoot) {
notifProcessing(noot)
} else if typeOfNoot == 51 {
self.mainUser.setBool(false, forKey: "hasChangedAvatar")
} else {
print("{SECO} - Problem here, not userKindOfShit, nor notifKindOfShit")
}
}
}
}
}
} else {
}
}
theBigBQuery.findObjectsInBackgroundWithBlock { (theNoots: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if theNoots!.count == 0 {
} else {
for noot in theNoots! {
theBigBQuery.getObjectInBackgroundWithId(noot.objectId!) { (thatNoot: PFObject?, error: NSError?) -> Void in
if error == nil {
thatNoot!.setValue(true, forKey: "seenB")
thatNoot!.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
} else {
}
})
let idOfTheGuy: String = thatNoot?.objectForKey("idOfA") as! String
var typeOfNoot: Int = Int(thatNoot!.objectForKey("type") as! NSNumber)
if typeOfNoot == 32 {
typeOfNoot = 33
}
print(typeOfNoot)
if userKindOfShit.contains(typeOfNoot) {
searchAndDestroying(idOfTheGuy)
if searchAndAppend.contains(typeOfNoot) {
searchAndAppending(idOfTheGuy, type: typeOfNoot)
}
} else if notifKindOfShit.contains(typeOfNoot) {
notifProcessing(noot)
} else if typeOfNoot == 51 {
changeSkin(idOfTheGuy)
} else {
}
}
}
}
}
} else {
}
}
}
我需要知道这整个" letsGo()"功能完成:执行两个查询时,是否有新的通知。我可以将该函数分成两个小函数,但我仍然会遇到同样的问题:如何使用执行背景块的函数完成?
感谢您的帮助。
修改
感谢Wain的宝贵帮助,我能够删除getObjectInBackgroundWithId
功能。现在是代码:
static func letsGo() {
print("{SECO} - m - main(Ignition)")
let userKindOfShit = [31, 32, 33, 41, 42, 43]
let searchAndAppend = [31, 32, 33]
let notifKindOfShit = [11, 12, 13, 14, 21, 22, 23, 24]
let theBigAQuery: PFQuery = PFQuery(className: "Notification")
let theBigBQuery: PFQuery = PFQuery(className: "Notification")
theBigAQuery.whereKey("nameA", equalTo: (PFUser.currentUser()?.username)!)
theBigBQuery.whereKey("nameB", equalTo: (PFUser.currentUser()?.username)!)
theBigAQuery.whereKey("seenA", equalTo: false)
theBigBQuery.whereKey("seenB", equalTo: false)
print("{SECO}m - still working up to here")
theBigAQuery.findObjectsInBackgroundWithBlock { (theNoots: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if theNoots!.count == 0 {
} else {
for noot in theNoots! {
noot.setValue(true, forKey: "seenA")
let typeOfNoot: Int = Int(noot.objectForKey("type") as! NSNumber)
let idOfTheGuy: String = noot.objectForKey("idOfB") as! String!
if userKindOfShit.contains(typeOfNoot) {
searchAndDestroying(idOfTheGuy)
if searchAndAppend.contains(typeOfNoot) {
searchAndAppending(idOfTheGuy, type: typeOfNoot)
}
} else if notifKindOfShit.contains(typeOfNoot) {
notifProcessing(noot)
} else if typeOfNoot == 51 {
self.mainUser.setBool(false, forKey: "hasChangedAvatar")
} else {
}
noot.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
}
})
}
}
} else {
}
}
theBigBQuery.findObjectsInBackgroundWithBlock { (theNoots: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if theNoots!.count == 0 {
} else {
for noot in theNoots! {
noot.setValue(true, forKey: "seenB")
let idOfTheGuy: String = noot.objectForKey("idOfA") as! String
var typeOfNoot: Int = Int(noot.objectForKey("type") as! NSNumber)
if typeOfNoot == 32 {
typeOfNoot = 33
}
if userKindOfShit.contains(typeOfNoot) {
searchAndDestroying(idOfTheGuy)
if searchAndAppend.contains(typeOfNoot) {
searchAndAppending(idOfTheGuy, type: typeOfNoot)
}
} else if notifKindOfShit.contains(typeOfNoot) {
notifProcessing(noot)
} else if typeOfNoot == 51 {
changeSkin(idOfTheGuy)
} else {
}
noot.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
} else {
}
})
}
}
} else {
}
}
}
答案 0 :(得分:1)
您应该创建一个包含要保存的所有对象的数组,然后使用PFObject API将它们全部保存在1个请求中。
您可以将所有部分链接在一起,以便在A请求完成并完成保存后启动B请求,然后从B保存回调中调用完成处理程序。
或者,您可以继续并行运行并使用调度组/信号量/简单计数器来了解所有请求何时完成。一个更好但更多代码的方法是将每个部分包装在一个异步操作中,然后将它们添加到另一个依赖于它们的操作的队列中,该操作包含完成处理的逻辑。