我有一个3D数组,通过一个枚举for循环执行本地SQL Lite表的后台查询。 99%的时间都是按顺序进行的。 1%的时间事情有点失序。结构阵列排序阵列[数组] [结构]
由于执行后台任务的代码包含在更接近的完成处理程序中,所以我不确定为什么序列会变得有趣。以下是结构和数组是如何形成的。
我在想排序数组,但不能确定语法,不知道这是否是最好的方法?这感觉就像一个工作。
struct CollectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
var packArray : [CollectionStruct] = []
var partArray : [CollectionStruct] = []
var multiPartArray : [[CollectionStruct]] = []
var tempPartArray : [[CollectionStruct]] = []
override func viewDidLoad() {
super.viewDidLoad()
// Built the self.packArray here in simalr fashion to below.
// Always in correct order as the query to build it is sorted
// Now build the partArray for each packArray
// access with
// self.multiPartArray[0][0].name
for (index, item) in self.packArray.enumerated() {
print(index, item)
// is it the index that is out of sequence?
self.packId = self.packArray[index].id
BuildArray.buildArrayFromQuery(queryForCollection: "Part", selectedPackID: self.packId, delegateSender: "DownloadPart", completeBlock: { (result) in
if result.isEmpty == false {
self.partArray = result
// this is how i was appending the result. But it will occasionaly append the results in the wrong order.
// self.packArray[1], self.packArray[0]. Not sure if its because of the index from the enumeration or what??
// self.multiPartArray.append(self.partArray)
// this is where i was thinking of sorting the array (or outside for loop) this will do for not to test
// set it to a temp so it can be sorted...
self.tempPartArray.append(self.partArray)
// now sort it.. dont know wtf to do here cant see anything online
self.multiPartArray = self.tempPartArray.sorted(by: { ([CollectionStruct], [CollectionStruct]) -> Bool in
<#code#>
})
}
})
}
}
我无法弄清楚如何排序这句法。在另一个位置我已经使用下面的代码进行排序的较小结构阵列:
self.packArray = result.sorted {
$0.name < $1.name
}
---- ---- EDIT
的提示输入.sorted
功能码如下,其中示出有是[CollectionStruct], [CollectionStruct]
但是,我的Struct中的属性(.name
等)在排序中不可用。
self.multiPartArray = self.tempPartArray.sorted(by: { ([CollectionStruct], [CollectionStruct]) -> Bool in
<#code#>
})
典型$ 0 < $ 1 ECT不可用或者作为我不能二进制运算符适用于2周的Structs ?? 理想情况下,我可以按.name排序,因为这是一个顺序标识符。所以排序
self.multiPartArray[0][0].name < self.multiPartArray[0][1].name < .... so on
这是数据的样本输出,当它是无序的,你可以看到,pk010是pk020后到来,其他的一切都是按顺序排列。
[[Proj.CollectionStruct(name: "pk000-01", description: "This is an intro session. ", title: "Session 1", image: <PFFile: 0x7b91dff0>, id: "JKE5K4xOQA"), Proj.CollectionStruct(name: "pk000-02", description: "This is a lot of text.", title: "Session 2", image: <PFFile: 0x7b892a30>, id: "ft4o3EWyxX"), Proj.CollectionStruct(name: "pk000-03", description: "This is session 3", title: "Session 3", image: <PFFile: 0x7a6fa5f0>, id: "jebz0c8Bq1"), Proj.CollectionStruct(name: "pk000-04", description: "This is a test", title: "Session 4", image: <PFFile: 0x7b88e390>, id: "E2rGHJMcR7"), Proj.CollectionStruct(name: "pk000-05", description: "This is a set", title: "Session 5", image: <PFFile: 0x7b892320>, id: "dkRAUEbdUb"), Proj.CollectionStruct(name: "pk000-06", description: "Test", title: "Session 6", image: <PFFile: 0x7a6f68a0>, id: "ODTJd3gIml")],
[Proj.CollectionStruct(name: "pk020-01", description: "This is an intro session", title: "Session 1", image: <PFFile: 0x7a6f7600>, id: "6uG2cJGxrZ"), Proj.CollectionStruct(name: "pk020-02", description: "Session 2", title: "Session 2", image: <PFFile: 0x7b878d60>, id: "6T6r8e8dS3"), Proj.CollectionStruct(name: "pk020-03", description: "Session 3", title: "Session 3", image: <PFFile: 0x7a7a0ff0>, id: "2hDSALAG0a"), Proj.CollectionStruct(name: "pk020-04", description: "Session 4", title: "Session 4", image: <PFFile: 0x7b910bd0>, id: "vd0eBYb3gr"), Proj.CollectionStruct(name: "pk020-05", description: "Session 5", title: "Session 5", image: <PFFile: 0x7b88f790>, id: "MS8ece8dr8"), Proj.CollectionStruct(name: "pk020-06", description: "Session 6", title: "Session 6", image: <PFFile: 0x7a7765c0>, id: "aDfmH8bFKU"), Proj.CollectionStruct(name: "pk020-07", description: "Session 7", title: "Session 7", image: <PFFile: 0x7b9eaf50>, id: "obfW0pZsBH"), Proj.CollectionStruct(name: "pk020-08", description: "Session 8", title: "Session 8", image: <PFFile: 0x7a783c20>, id: "cVmEefiirT")],
[Proj.CollectionStruct(name: "pk010-01", description: "This is an intro session", title: "Session 1", image: <PFFile: 0x7a6f9b20>, id: "BPtmDXvzDF"), Proj.CollectionStruct(name: "pk010-02", description: "Session 2", title: "Session 2", image: <PFFile: 0x7a79ac00>, id: "B3MAHoFEYs"), Proj.CollectionStruct(name: "pk010-03", description: "Session 3", title: "Session 3", image: <PFFile: 0x7a627e40>, id: "Yg6TxTuyHi"), Proj.CollectionStruct(name: "pk010-04", description: "Session 4", title: "Session 4", image: <PFFile: 0x7a627f90>, id: "XTRUoKVH9P"), Proj.CollectionStruct(name: "pk010-05", description: "Session 5", title: "Session 5", image: <PFFile: 0x7a627b80>, id: "eO9xS05qsW"), Proj.CollectionStruct(name: "pk010-06", description: "Session 6", title: "Session 6", image: <PFFile: 0x7b879bd0>, id: "ePZdmFnnoQ"), Proj.CollectionStruct(name: "pk010-07", description: "Session 7", title: "Session 7", image: <PFFile: 0x7be58c50>, id: "BO6HGa7w1R"), Proj.CollectionStruct(name: "pk010-08", description: "Session 8", title: "Session 8", image: <PFFile: 0x7a69bd20>, id: "dwIxpmD2y8")],
[Proj.CollectionStruct(name: "pk030-01", description: "Session 3", title: "Session 3", image: <PFFile: 0x7b87da60>, id: "lU1rm5cnbb")]]
-----编辑----
我还一直无法理清这一点,但我一直在玩至少捕获了错误,它不是太斯文了其在另一枚枚举循环中,但它是目前我的大脑中最好的。
// add a group for the for enumeration check
let insideGroup = DispatchGroup()
for (index, item) in self.tempPartArray.enumerated() {
insideGroup.enter()
if index >= 1 {
// if the currentItem.name > the previousItem.name
if item[0].name > self.tempPartArray[index - 1][0].name {
print("The array is in sequence")
insideGroup.leave()
} else {
print("the array is out of sequence")
// exit gracefully
}
}
}
// the enumeration has finished you can set the variables
insideGroup.notify(queue: .main) {
self.multiPartArray = self.tempPartArray
}
-----编辑----
甚至重新编写查询是同步的任务仍然返回数据失序作为一个查询可能需要比另一个更长。
我想我需要在这里做的就是一个操作的查询部分有axConcurrentOperationCount = 1
这应该迫使成果转化序列。
编号仍想知道如何将这个类型的数组虽然进行排序....
谢谢,查询确实得到的对象异步。我很天真的感觉,因为它是通过完成块完成的,所以它会按顺序返回。就像你说的我可以得到所有的对象/数组,然后处理它,我需要。我尝试在for循环中使用dispatchGroup进入/离开和完成来强制它同步,但它确实减慢了一点,这可能会导致在集合变大时锁定线程。有了这种排序,Xcode促使我使用.sorted(通过:{[..],[..]} - >我不知道。 – WanderingScouse
它是给你的排序是只是什么是在我的回答,如果方法是异步,这可能是为什么它有时会失序,你可能需要写一个新的方法来同时查询你需要的所有东西,或者等待并排序。 – PeejWeej