GocePetrovski / ScrollableSegmentedControl

Scrollable Segmented Control for when UISegmentedControl is not sufficient
MIT License
245 stars 74 forks source link

Inserting Segments from Network Request Array Broken Behavior #25

Closed chuninator closed 6 years ago

chuninator commented 6 years ago

Aloha,

Running into a problem when I load the needed segments in from an array, as opposed to doing it statically.

This function works and gives the desired output. All 5 show up in the segmentedControl.

func testCategory() {
        var int = 0
        for i in 1...5 {
            print("here is \(int)")
            segmentedControl.insertSegment(withTitle: "Sssss", at: int)
            int += 1
        }
    }

Photo: screen shot 2018-08-27 at 1 20 54 pm

On the other hand, when making a request from our database, here's what happens. The segments show up individually instead of filling the entire screen with x amount that we need.

   func loadCategories() {

        var int = 0
        segmentedControl.segmentStyle = .textOnly
        segmentedControl.underlineSelected = true

        let queryForCategories = PFQuery(className: Category.parseClassName())
        queryForCategories.whereKey("active", equalTo: true)
        queryForCategories.findObjectsInBackground { (categories, error) in
            if error == nil {
                self.categoriesArray.removeAll()
                self.categoriesArray = Array(categories!.makeIterator()) as! [Category]

                for category in self.categoriesArray {
                    let name = category.name
                    DispatchQueue.main.async {
                        self.segmentedControl.insertSegment(withTitle: name, at: int)
                    }
                }

            } else {
                switch error!._code {
                case 100:
                    print("networking error.")
                default: fatalError()
                }
            }
        }
    }

Photo: screen shot 2018-08-27 at 1 21 36 pm

Any help greatly appreciated. Thank you.

GocePetrovski commented 6 years ago

Try running this whole block of code on main thread.

            self.categoriesArray.removeAll()
            self.categoriesArray = Array(categories!.makeIterator()) as! [Category]

            for category in self.categoriesArray {
                let name = category.name
                DispatchQueue.main.async {
                    self.segmentedControl.insertSegment(withTitle: name, at: int)
                }
            }
chuninator commented 6 years ago

@GocePetrovski Hm, no luck... any other ideas? I also tried appending to the array one by one as opposed to makeIterator()

chuninator commented 6 years ago

Fixed it. Really nothing to do with the ScrollableSegmentedControl. For some reason even DispatchQueue.main.async {} wasn't actually executing code on main thread. Used a different function findObjects() for finding objects synchronously.

func loadCategories() {
        let queryForCategories = PFQuery(className: Category.parseClassName())
        queryForCategories.whereKey("active", equalTo: true)

        do {
            let results: [Category] = try queryForCategories.findObjects() as! [Category]
            print("These are the results \(results)")
            for r in results {
                categoriesArray.append(r.name)
                self.segmentedControl.insertSegment(withTitle: r.name, at: 0)
            }
        } catch {
            print(error)
        }
    }

I guess just make sure your results are always being inserted on main thread. UI update so it makes sense. Thank you for the time and mind space.