我正在尝试在xcode中构造一个解析查询,它接受通过前一个segue传递给视图控制器的对象数组,从这些对象创建一个objectId数组,并查询数组objectId中的完全匹配“MessageThread”类中的键“参与者”中的值
我一直在阅读解析文档,我能提出的最佳查询是containsAllObjectsIn:
显然,containsAllObjectsIn在某些条件下会起作用。但是,该查询仅检查键“参与者”中是否存在所有objectId。它不检查阵列是否相同。因此,查询将返回对象,虽然它们包含所有objectIds,但可能包含额外的objectIds。
为了对付这种负面影响,我对每个返回的对象进行检查,以查看返回对象中的objectIds是否与原始数组完全匹配。我已经在下面包含了视图控制器代码。
虽然我目前在下面解决的代码似乎在测试后按预期工作,但我不禁要知道必须有一种更有效的方法来检查数组是否通过PFQuery是相同的。< / p>
有关如何使此代码更好的任何想法或帮助?感谢。
import UIKit
import Parse
class MessageThreadViewController: UIViewController {
@IBOutlet weak var sendNewMessageButton: UIButton!
var participantsList:[PFObject] = []
var participantsListIds:[String] = []
var currentMessageThreadObject:PFObject?
var threadMessages:[PFObject] = []
override func viewDidLoad() {
super.viewDidLoad()
initialViewSetup()
checkForExistingMessageThread()
}
func initialViewSetup() {
sendNewMessageButton.isEnabled = false
for participant in participantsList {
participantsListIds.append(participant.objectId!)
}
}
func checkForExistingMessageThread () {
print("-------------------")
print(participantsListIds)
let query = PFQuery(className: "MessageThread")
query.whereKey("participants", containsAllObjectsIn: participantsList)
query.includeKey("messages")
query.findObjectsInBackground(block: { (threads, error) in
if error != nil {
if let error = error {
print(error)
}
} else {
if let threads = threads {
if threads.isEmpty {
print("no thread found. creating thread....")
self.createMessageThread()
} else {
print("existing thread match(es) found")
var activeThreadFound = false
for thread in threads {
let returnedThreadParticipants = thread["participants"] as! [PFObject]
var returnedThreadParticipantsIds:[String] = []
for participant in returnedThreadParticipants {
returnedThreadParticipantsIds.append(participant.objectId!)
}
if returnedThreadParticipantsIds.containsSameElements(as: self.participantsListIds) {
activeThreadFound = true
print("thread identical match found")
self.currentMessageThreadObject = thread
self.sendNewMessageButton.isEnabled = true
if let queriedThreadMessages = thread["messages"] {
let queriedThreadMessagesAsObjects = queriedThreadMessages as! [PFObject]
self.threadMessages = queriedThreadMessagesAsObjects
self.messagesTableView.reloadData()
self.sendNewMessageButton.isEnabled = true
}
break
} else {
print("non-identical thread found")
}
}
if activeThreadFound == false {
print("matches were found, but no identical thread. Creating thread....")
self.createMessageThread()
}
}
}
}
})
}
func createMessageThread() {
let newMessageThread = PFObject(className:"MessageThread")
newMessageThread["participants"] = participantsList
newMessageThread.saveInBackground(block: { (success, error) in
if error != nil {
if let error = error {
print(error)
}
} else {
self.checkForExistingMessageThread()
}
})
}
}
extension Array where Element: Comparable {
func containsSameElements(as other: [Element]) -> Bool {
return self.count == other.count && self.sorted() == other.sorted()
}
答案 0 :(得分:0)
我的另一个建议是尝试在同一个查询中组合containsIn和containsAllObjectsInArray。请尝试这样的事情:
let query = PFQuery(className: "MessageThread")
query.whereKey("participants", containsAllObjectsInArray: participantsList)
query.whereKey("participants", containedIn: participantsList)