我有一個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