gpt4 book ai didi

multithreading - 通过 block 完成在后台查找对象

转载 作者:行者123 更新时间:2023-11-30 10:09:36 25 4
gpt4 key购买 nike

我正在使用 swift 和 parse 构建一个应用程序。我想实现“下拉​​”刷新。只要我的应用程序查询服务器,这就会触发一个小动画(类似 snapchat)。为了查询服务器,我有一个带有执行两个后台 block 的函数的类:

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()”函数何时完成:两个查询何时执行,是否有新的 notif。我可以将该函数分成两个小函数,但我仍然会遇到同样的问题:如何通过执行后台 block 的函数获得完成?

感谢您的帮助。

编辑:

感谢 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 {
}
}
}

最佳答案

您应该创建一个数组来保存所有要保存的对象,然后您可以使用 PFObject API 将它们全部保存在 1 个请求中。

您可以将所有部分链接在一起,以便 B 请求在 A 请求完成并保存完成后启动,然后从 B 保存回调中调用完成处理程序。

或者,您可以继续并行运行,并使用调度组/信号量/简单计数器来了解所有请求何时完成。一种更好但更多代码的方法是将每个部分包装在异步操作中,然后将它们添加到另一个操作的队列中,该操作依赖于它们全部并包含完成处理的逻辑。

关于multithreading - 通过 block 完成在后台查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707222/

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