diff --git a/Source/Core.swift b/Source/Core.swift index b781b2266..ffc972007 100644 --- a/Source/Core.swift +++ b/Source/Core.swift @@ -513,11 +513,13 @@ extension Form : RangeReplaceableCollectionType { kvoWrapper.sections.replaceObjectsInRange(NSMakeRange(subRange.startIndex, subRange.endIndex - subRange.startIndex), withObjectsFromArray: newElements.map { $0 }) kvoWrapper._allSections.insertContentsOf(newElements, at: indexForInsertionAtIndex(subRange.startIndex)) - for section in newElements{ - section.wasAddedToForm(self) - } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in + for section in newElements{ + section.wasAddedToForm(self) + } + } } - + public func removeAll(keepCapacity keepCapacity: Bool = false) { // not doing anything with capacity for section in kvoWrapper._allSections{ @@ -1442,9 +1444,13 @@ extension BaseRow { assert(section.form?.rowsByTag[t] == nil, "Duplicate tag \(t)") self.section?.form?.rowsByTag[t] = self } - addToRowObservers() - evaluateHidden() - evaluateDisabled() + if self.section?.form != nil { + self.addToRowObservers() + dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in + self?.evaluateHidden() + self?.evaluateDisabled() + } + } } private final func addToHiddenRowObservers() { diff --git a/Tests/FormValuesTests.swift b/Tests/FormValuesTests.swift index 6b351da10..598e0cec5 100644 --- a/Tests/FormValuesTests.swift +++ b/Tests/FormValuesTests.swift @@ -63,16 +63,18 @@ class FormValuesTests: BaseEurekaTests { let textRowInvisible = TextRow("InvisibleRowTag") textRowInvisible.hidden = true form +++ Section() <<< textRowInvisible - - XCTAssertEqual(form.values(includeHidden: true).count, 2) - XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String) - - XCTAssertEqual(form.allRows.count, 3) - XCTAssertEqual(form.rows.count, 2) - XCTAssertEqual(form.values().count, 1) - XCTAssertEqual(form.values(includeHidden: true).count, 2) + + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(form.values(includeHidden: true).count, 2) + XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String) + + XCTAssertEqual(form.allRows.count, 3) + XCTAssertEqual(form.rows.count, 2) + XCTAssertEqual(form.values().count, 1) + XCTAssertEqual(form.values(includeHidden: true).count, 2) + } } - + func testIncludeHiddenFormValues() { let form = Form() @@ -100,16 +102,18 @@ class FormValuesTests: BaseEurekaTests { textRowInvisible.hidden = true textRowInvisible.value = "Bye!" form +++ Section() <<< textRowInvisible - - XCTAssertEqual(form.values(includeHidden: true).count, 2) - XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String) - XCTAssertTrue(form.values(includeHidden: true)["InvisibleRowTag"] is String) - - XCTAssertEqual(form.allRows.count, 3) - XCTAssertEqual(form.rows.count, 2) - XCTAssertEqual(form.values().count, 1) - XCTAssertEqual(form.values(includeHidden: true).count, 2) + + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(form.values(includeHidden: true).count, 2) + XCTAssertTrue(form.values(includeHidden: true)["RowTag"] is String) + XCTAssertTrue(form.values(includeHidden: true)["InvisibleRowTag"] is String) + + XCTAssertEqual(form.allRows.count, 3) + XCTAssertEqual(form.rows.count, 2) + XCTAssertEqual(form.values().count, 1) + XCTAssertEqual(form.values(includeHidden: true).count, 2) + } } - - + + } diff --git a/Tests/HelperMethodTests.swift b/Tests/HelperMethodTests.swift index d27dcb91d..241d2e8ab 100644 --- a/Tests/HelperMethodTests.swift +++ b/Tests/HelperMethodTests.swift @@ -96,20 +96,21 @@ class HelperMethodTests: BaseEurekaTests { let intRow = IntRow("int"){ $0.disabled = true } formVC.form +++ checkRow <<< switchRow <<< segmentedRow <<< intRow - - checkRow.updateCell() - XCTAssertTrue(checkRow.cell.selectionStyle == .None) - - switchRow.updateCell() - XCTAssertNotNil(switchRow.cell.switchControl) - XCTAssertFalse(switchRow.cell.switchControl!.enabled) - - segmentedRow.updateCell() - XCTAssertFalse(segmentedRow.cell.segmentedControl.enabled) - - intRow.updateCell() - XCTAssertFalse(intRow.cell.cellCanBecomeFirstResponder()) - + + dispatch_async(dispatch_get_main_queue()) { () -> Void in + checkRow.updateCell() + XCTAssertTrue(checkRow.cell.selectionStyle == .None) + + switchRow.updateCell() + XCTAssertNotNil(switchRow.cell.switchControl) + XCTAssertFalse(switchRow.cell.switchControl!.enabled) + + segmentedRow.updateCell() + XCTAssertFalse(segmentedRow.cell.segmentedControl.enabled) + + intRow.updateCell() + XCTAssertFalse(intRow.cell.cellCanBecomeFirstResponder()) + } } - + } diff --git a/Tests/HiddenRowsTests.swift b/Tests/HiddenRowsTests.swift index 074eefc0e..2117b07dd 100644 --- a/Tests/HiddenRowsTests.swift +++ b/Tests/HiddenRowsTests.swift @@ -262,29 +262,32 @@ class HiddenRowsTests: BaseEurekaTests { s2 sec2 (2 rows) */ - XCTAssertEqual(form.count, 5) XCTAssertEqual(form[0].count, 2) XCTAssertEqual(form[1].count, 0) XCTAssertEqual(form[2].count, 2) XCTAssertEqual(form[3].count, 0) XCTAssertEqual(form[4].count, 2) - - form[0][0].baseValue = "hello, good morning!" - - XCTAssertEqual(form.count, 3) - XCTAssertEqual(form[0].count, 2) - XCTAssertEqual(form[1].count, 2) - XCTAssertEqual(form[2].count, 2) - - form[0][0].baseValue = "whatever" - - XCTAssertEqual(form.count, 5) - XCTAssertEqual(form[1].tag, "s1_ths") - XCTAssertEqual(form[3].tag, "s2_ths") - XCTAssertEqual(form[4].tag, "s3_hrt") + + form[0][0].baseValue = "hello, good morning!" + + dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in + XCTAssertEqual(self?.form.count, 3) + XCTAssertEqual(self?.form[0].count, 2) + XCTAssertEqual(self?.form[1].count, 2) + XCTAssertEqual(self?.form[2].count, 2) + + self?.form[0][0].baseValue = "whatever" + + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(self?.form.count, 5) + XCTAssertEqual(self?.form[1].tag, "s1_ths") + XCTAssertEqual(self?.form[3].tag, "s2_ths") + XCTAssertEqual(self?.form[4].tag, "s3_hrt") + } + } } - + func testInsertionIndex(){ let r1 = CheckRow("check1_tii_hrt"){ $0.hidden = "$NameRow_s1 contains 'morning'" } let r2 = CheckRow("check2_tii_hrt"){ $0.hidden = "$NameRow_s1 contains 'morning'" } @@ -309,10 +312,12 @@ class HiddenRowsTests: BaseEurekaTests { // insert another row form[1].insert(r4, atIndex: 1) - XCTAssertEqual(form[1].count, 2) // all inserted rows should be hidden - XCTAssertEqual(form[1][0].tag, "int1_hrt") - XCTAssertEqual(form[1][1].tag, "txt1_hrt") - + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(self.form[1].count, 2) // all inserted rows should be hidden + XCTAssertEqual(self.form[1][0].tag, "int1_hrt") + XCTAssertEqual(self.form[1][1].tag, "txt1_hrt") + } + form[0][0].baseValue = "whatever" // we inserted r4 at index 1 but there were two rows hidden before it as well so it shall be at index 3 @@ -331,16 +336,19 @@ class HiddenRowsTests: BaseEurekaTests { //inserting 2 rows at the end, deleting 1 form[2].replaceRange(Range(start: 1, end: 2), with: [r2, r4]) - XCTAssertEqual(form[1].count, 0) - XCTAssertEqual(form[2].count, 1) - XCTAssertEqual(form[2][0].tag, "txt2_hrt") - - form[0][0].baseValue = "whatever" - - XCTAssertEqual(form[2].count, 3) - XCTAssertEqual(form[2][0].tag, "txt2_hrt") - XCTAssertEqual(form[2][1].tag, "check2_tii_hrt") - XCTAssertEqual(form[2][2].tag, "check4_tii_hrt") - + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(self.form[1].count, 0) + XCTAssertEqual(self.form[2].count, 1) + XCTAssertEqual(self.form[2][0].tag, "txt2_hrt") + + self.form[0][0].baseValue = "whatever" + + dispatch_async(dispatch_get_main_queue()) { () -> Void in + XCTAssertEqual(self.form[2].count, 3) + XCTAssertEqual(self.form[2][0].tag, "txt2_hrt") + XCTAssertEqual(self.form[2][1].tag, "check2_tii_hrt") + XCTAssertEqual(self.form[2][2].tag, "check4_tii_hrt") + } + } } }