Swift iOS9 - dispatch_group - 需要代码以特定顺序执行
Swift iOS9 - dispatch_group - need code to execute in specific order
感谢您单击此线程以帮助编写代码和提供反馈。
我的代码没有按预期工作。我需要代码按照输入的顺序运行。目前只有前 2 个部分在按需要工作。 查看代码中的注释以了解更多详细信息。
tl;dr 如何让注释部分按照我在注释中键入的顺序执行?
func getResults() {
resultsFollowingArray.removeAll(keepCapacity: false)
resultsDisplayNameArray.removeAll(keepCapacity: false)
resultsProfilePicArray.removeAll(keepCapacity: false)
resultsNoticeReasonArray.removeAll(keepCapacity: false)
resultsNoticeDescriptionArray.removeAll(keepCapacity: false)
resultsDatePostedArray.removeAll(keepCapacity: false)
let followGroup: dispatch_group_t = dispatch_group_create()
let profilePicGroup: dispatch_group_t = dispatch_group_create()
let noticesGroup: dispatch_group_t = dispatch_group_create()
/*** needs to execute first ***/
dispatch_group_async(followGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
print(self.resultsFollowingArray) //works fine
} catch _ {
}
})
/*** needs to execute second ***/
dispatch_group_notify(followGroup, dispatch_get_main_queue(), {
if (self.resultsFollowingArray.count > 0) {
dispatch_group_async(profilePicGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
}
}
print(self.resultsProfilePicArray) //works fine
})
}
})
/*** needs to execute third ***/
/*** this section seems to be the first section to execute, therefore it is not working as I need it ***/
dispatch_group_notify(profilePicGroup, dispatch_get_main_queue(), {
print(self.resultsFollowingArray) //prints empty array
if (self.resultsFollowingArray.count > 0) {
dispatch_group_async(noticesGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
self.tvMyFeed.reloadData()
print(self.resultsDisplayNameArray) //prints nothing
} catch _ {
}
}
})
}
})
/*** needs to execute last ***/
dispatch_group_notify(noticesGroup, dispatch_get_main_queue(), {
self.tvMyFeed.reloadData()
})
}
更新代码:由于某些奇怪的原因,PFQuery "Notices" 被调用了两次,它把一切都搞砸了。这就是我想要做的,但我正在尝试这样做 "in the background," 此代码仍然冻结 UI:
let group: dispatch_group_t = dispatch_group_create()
dispatch_group_enter(group)
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
dispatch_group_leave(group)
} catch _ {
dispatch_group_leave(group)
}
dispatch_group_enter(group)
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
dispatch_group_leave(group)
}
}
dispatch_group_leave(group)
dispatch_group_enter(group)
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
dispatch_group_leave(group)
} catch _ {
dispatch_group_leave(group)
}
}
dispatch_group_notify(group, dispatch_get_main_queue(), {
self.tvMyFeed.reloadData()
})
我找到了解决办法。将自己的生命奉献给这个目的的你,请随意 post 你的,我很乐意接受你的智慧,只要它能按需运作。
这是我一直在寻找的最终解决方案(感谢上帝,我能够自己找到它!):效果非常好!
func getResults() {
resultsFollowingArray.removeAll(keepCapacity: false)
resultsDisplayNameArray.removeAll(keepCapacity: false)
resultsProfilePicArray.removeAll(keepCapacity: false)
resultsNoticeReasonArray.removeAll(keepCapacity: false)
resultsNoticeDescriptionArray.removeAll(keepCapacity: false)
resultsDatePostedArray.removeAll(keepCapacity: false)
let followGroup: dispatch_group_t = dispatch_group_create()
dispatch_group_async(followGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
if (obj.valueForKey("followed") as! String != myEmail) {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
}
if (self.resultsFollowingArray.count > 0) {
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
self.tvMyFeed.reloadData()
} catch _ {
}
}
}
} catch _ {
}
})
dispatch_group_notify(followGroup, dispatch_get_main_queue(), {
if (self.resultsFollowingArray.count > 0) {
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
}
}
self.tvMyFeed.reloadData()
}
})
}
感谢您单击此线程以帮助编写代码和提供反馈。
我的代码没有按预期工作。我需要代码按照输入的顺序运行。目前只有前 2 个部分在按需要工作。 查看代码中的注释以了解更多详细信息。
tl;dr 如何让注释部分按照我在注释中键入的顺序执行?
func getResults() {
resultsFollowingArray.removeAll(keepCapacity: false)
resultsDisplayNameArray.removeAll(keepCapacity: false)
resultsProfilePicArray.removeAll(keepCapacity: false)
resultsNoticeReasonArray.removeAll(keepCapacity: false)
resultsNoticeDescriptionArray.removeAll(keepCapacity: false)
resultsDatePostedArray.removeAll(keepCapacity: false)
let followGroup: dispatch_group_t = dispatch_group_create()
let profilePicGroup: dispatch_group_t = dispatch_group_create()
let noticesGroup: dispatch_group_t = dispatch_group_create()
/*** needs to execute first ***/
dispatch_group_async(followGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
print(self.resultsFollowingArray) //works fine
} catch _ {
}
})
/*** needs to execute second ***/
dispatch_group_notify(followGroup, dispatch_get_main_queue(), {
if (self.resultsFollowingArray.count > 0) {
dispatch_group_async(profilePicGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
}
}
print(self.resultsProfilePicArray) //works fine
})
}
})
/*** needs to execute third ***/
/*** this section seems to be the first section to execute, therefore it is not working as I need it ***/
dispatch_group_notify(profilePicGroup, dispatch_get_main_queue(), {
print(self.resultsFollowingArray) //prints empty array
if (self.resultsFollowingArray.count > 0) {
dispatch_group_async(noticesGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
self.tvMyFeed.reloadData()
print(self.resultsDisplayNameArray) //prints nothing
} catch _ {
}
}
})
}
})
/*** needs to execute last ***/
dispatch_group_notify(noticesGroup, dispatch_get_main_queue(), {
self.tvMyFeed.reloadData()
})
}
更新代码:由于某些奇怪的原因,PFQuery "Notices" 被调用了两次,它把一切都搞砸了。这就是我想要做的,但我正在尝试这样做 "in the background," 此代码仍然冻结 UI:
let group: dispatch_group_t = dispatch_group_create()
dispatch_group_enter(group)
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
dispatch_group_leave(group)
} catch _ {
dispatch_group_leave(group)
}
dispatch_group_enter(group)
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
dispatch_group_leave(group)
}
}
dispatch_group_leave(group)
dispatch_group_enter(group)
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
dispatch_group_leave(group)
} catch _ {
dispatch_group_leave(group)
}
}
dispatch_group_notify(group, dispatch_get_main_queue(), {
self.tvMyFeed.reloadData()
})
我找到了解决办法。将自己的生命奉献给这个目的的你,请随意 post 你的,我很乐意接受你的智慧,只要它能按需运作。
这是我一直在寻找的最终解决方案(感谢上帝,我能够自己找到它!):效果非常好!
func getResults() {
resultsFollowingArray.removeAll(keepCapacity: false)
resultsDisplayNameArray.removeAll(keepCapacity: false)
resultsProfilePicArray.removeAll(keepCapacity: false)
resultsNoticeReasonArray.removeAll(keepCapacity: false)
resultsNoticeDescriptionArray.removeAll(keepCapacity: false)
resultsDatePostedArray.removeAll(keepCapacity: false)
let followGroup: dispatch_group_t = dispatch_group_create()
dispatch_group_async(followGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let fQ = PFQuery(className: "Following")
fQ.limit = 30
fQ.whereKey("follower", equalTo: myEmail)
do {
let objs = try fQ.findObjects()
for obj in objs {
if (obj.valueForKey("followed") as! String != myEmail) {
self.resultsFollowingArray.append(obj.valueForKey("followed") as! String)
}
}
if (self.resultsFollowingArray.count > 0) {
let nQ = PFQuery(className: "Notices")
nQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
nQ.whereKey("poster", equalTo: self.resultsFollowingArray[i])
do {
let notices = try nQ.findObjects()
for notice in notices {
self.resultsDisplayNameArray.append(notice.valueForKey("poster_display_name") as! String)
self.resultsNoticeReasonArray.append(notice.valueForKey("reason") as! String)
self.resultsNoticeDescriptionArray.append(notice.valueForKey("notice_description") as! String)
self.resultsDatePostedArray.append(notice.valueForKey("date_posted") as! String)
}
self.tvMyFeed.reloadData()
} catch _ {
}
}
}
} catch _ {
}
})
dispatch_group_notify(followGroup, dispatch_get_main_queue(), {
if (self.resultsFollowingArray.count > 0) {
let uQ = PFQuery(className: "_User")
uQ.limit = 30
for (var i = 0; i < self.resultsFollowingArray.count; i++) {
uQ.whereKey("username", equalTo: self.resultsFollowingArray[i])
do {
try self.resultsProfilePicArray.append(uQ.getFirstObject().valueForKey("profile_picture") as! PFFile)
} catch _ {
}
}
self.tvMyFeed.reloadData()
}
})
}