Search code examples
macoscocoaswift3nsprogressindicator

swift progessindicator for OS X


i have a loop, which shows me all my address book contacts. now i would like to show an NSProgressIndicator, which should show the progress from 0 (=start request contacts) until 100 (=all contacts requested). for this I tried something like this:

let addressBook = ABAddressBook.shared()
let people = addressBook?.people()
for person in people!  as! [ABPerson] {
   print(person)
   ProgressIndicator.increment(by: 0.22)
}

why increment by 0.22?

i have 451 contacts 100/451 = 0,22% per contact

but what happens? i start my app. the print result will show each contact and when all contacts were requested > the progress indicator jumps to 100% directly.

what do I wrong?

Second try:

DispatchQueue.global(qos: .background).async {
   let addressBook = ABAddressBook.shared()
   let people = addressBook?.people()
   let stepPercent = 100 / (people?.count)!
   for person in people!  as! [ABPerson] {
      print(person)
      DispatchQueue.main.async {
         self.ProgressIndicator.increment(by: Double(stepPercent))
   }
}

Solution

  • My Solution:

    DispatchQueue.global(qos: .background).async {
                let addressBook = ABAddressBook.shared()
                let people = addressBook?.people()
                print(Double((people?.count)!))
                let stepPercent = 100.00 / (Double((people?.count)!))
                print(stepPercent)
                for person in people!  as! [ABPerson] {
                    print(person)
                DispatchQueue.main.async {
                    self.ProgressIndicator.increment(by: Double(stepPercent))
                }
            }
    

    thanks for support