ローカルSQL Liteテーブルのバックグラウンドクエリを実行する列挙型forループを使用して構築された3D配列があります。すべての時間が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#>
})
}
})
}
}
これを並べ替える方法の構文を理解できません。 iが小さい構造体のアレイをソートするために以下のコードを使用している別の場所に:
self.packArray = result.sorted {
$0.name < $1.name
}
---- ---- EDIT
ザは、以下に示すように、.sorted
関数のコードはここであり、ある促さ[CollectionStruct], [CollectionStruct]
ですが、My Struct(.name
)のプロパティはソートで使用できません。 iが2つの構造体にバイナリ演算子を適用することができないように
self.multiPartArray = self.tempPartArray.sorted(by: { ([CollectionStruct], [CollectionStruct]) -> Bool in
<#code#>
})
典型的$ 0 < $ 1 ECTは、いずれか利用できません? 理想的には、これはシーケンシャルな識別子であるため、.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")]]
----- EDIT ----
私はまだこれを並べ替えることができませんでしたが、私は少なくともエラーをキャッチしてプレーしてきた、そのようにそのあまりにも優雅ではありません別の列挙されたループでは、その最高の私の脳は今のところあります。 1つのクエリが長く、他よりもかかる可能性がありますよう
// 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
}
----- EDIT ----
がさえクエリは、同期タスクであることを再書き込みはまだシーケンス外のデータを返します。私はここで何をする必要があるかと思います
は、シーケンスに結果を強制する必要がありますaxConcurrentOperationCount = 1
と操作のクエリ部分を作るです。
Idはまだ
ありがとう、クエリは、非同期オブジェクトを取得します。私は、それが完了ブロックを介して行われたので、順番に戻ってくるだろうという印象のもとでは、素朴にいました。あなたが言ったように、私はすべてのオブジェクト/配列を取得してから、それを処理することができます。私はdispatchGroupの入力/終了とforループで同期を強制的に完了することを試みたが、それは少し遅くなり、これはおそらくコレクションが大きくなるとスレッドをロックする可能性があります。 Xcodeは並べ替えを使って.sorted(by:{[..]、[..]} - >私は理解できません)を使用するように促しています – WanderingScouse
あなたが並べ替えるのは、私の答えです。メソッドが非同期の場合は、おそらく時折順不同に終わることがあります。同時に必要なものをすべて照会するか、待ってから並べ替える新しいメソッドを作成する必要があります。 – PeejWeej