0

I want to print the data only after executing all loops inside the closure block, since I don't know how to use dispatch group exactly. I have tried in several ways.

What am I doing wrong here?

private func recognizeText(images: [UIImage]) {
    
    let myDispatchGroup = DispatchGroup()
    
    self.extractedTextfromImages(images: images) { blocks in
        myDispatchGroup.enter()
        
        for block in blocks {
            
            for line in block.lines {
                
                
                //save emirate id
                if isValidEmiratesID(emiratesID: line.text) == true {
                    
                    let id  = line.text
                    self.dt.id = id
                }
                
                // save name
                if line.text.lowercased().range(of: "name") != nil {
                    
                    
                    if let range = line.text.range(of: ":") {
                        let nm = line.text[range.upperBound...]
                        let name = String(nm.trimmingCharacters(in: .whitespaces))
                        self.dt.name = name
                    }
                }
                
            }
            
        }
        myDispatchGroup.leave()
    }
    
    myDispatchGroup.notify(queue: .main) {
        print("data is \(self.dt)")
    }
    
    
    
}
3
  • I think before this myDispatchGroup.leave() you can print . Commented May 4, 2021 at 9:10
  • can your place rewrite my code and post as answer 😊 Commented May 4, 2021 at 9:14
  • DispatchGroup makes no sense in the context of a single asynchronous task. Commented May 5, 2021 at 13:26

1 Answer 1

1
myDispatchGroup.enter()
              
        for i in 1...3 {
            
         print("out")
            for i in 1...3 {       
                     print("inner")   
                           } 
                       }
                myDispatchGroup.leave()
            
            
            myDispatchGroup.notify(queue: .main) {
                print(“now loop ended ")
            
    }

Output =

out

inner

inner

inner

out

inner

inner

inner

out

inner

inner

inner

now loop ended

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.