Updating a variable only after a function stops running

Hey again, I recently asked about updating a variable but now have a new problem. I am creating an NFC reader which when I press scan it prints the UID (Unique Identifier) of the NFC in a text field. When the button labeled "Scan" is pressed, the function activate() runs which activates the NFC reader and updates a variable within the NFCReader class called tagUID to the UID of the NFC chip. After activate() the button then changes tagCode to equal tagUID. The problem is that I want it to update only after the NFCTagReaderSession ends, as it only updates it to the previous UID scanned right now as it did not finish scanning before I update the variable. How do I do this? The code is below:


struct ContentView: View {

    let reader: NFCReader = NFCReader()

    @State var tagCode = "UID"

    var body: some View {

        VStack{

            Text(tagCode)

            Button {

                reader.activate()

                tagCode = reader.tagUID

            } label: {

                Text("Scan")

                    .font(.title)

            }.padding()

           

    }

}

}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}



class NFCReader: NSObject, ObservableObject, NFCTagReaderSessionDelegate {

    var tagUID = "UID"

    var session: NFCTagReaderSession?

    func activate() {

    self.session = NFCTagReaderSession(pollingOption: .iso14443, delegate: self)

    self.session?.alertMessage = "Hold Your Phone Near the NFC Tag"

    self.session?.begin()

    }

func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {

    print("Session Begun!")

}

func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {

    print("Error with Launching Session")

}

func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {

 print("Connecting To Tag")

    if tags.count > 1{

        session.alertMessage = "More Than One Tag Detected, Please try again"

        session.invalidate()

    }

    let tag = tags.first!

    session.connect(to: tag) { [self] (error) in

        if nil != error{

            session.invalidate(errorMessage: "Connection Failed")

        }

        if case let .miFare(sTag) = tag{

            let UID = sTag.identifier.map{ String(format: "%.2hhx", $0)}.joined()

            print("UID:", UID)

            print(sTag.identifier)

            session.alertMessage = "UID Captured"

            session.invalidate()

            DispatchQueue.main.async {

                self.tagUID = "\(UID)"

            }

            

        }

        

    }



}

   

}
Answered by 00ri in 702206022

I was looking into completion handlers but the problem is that I would need to define ContentView in the NFCReader class and when I do that the program stops responding so I assume that it does not like that. How would I get around this?

Did you consider using async / wait pattern ? Or using completion handlers ?

See:

h t t p s : / / w w w.raywenderlich.com/9582458-getting-started-with-core-nfc

Accepted Answer

I was looking into completion handlers but the problem is that I would need to define ContentView in the NFCReader class and when I do that the program stops responding so I assume that it does not like that. How would I get around this?

Updating a variable only after a function stops running
 
 
Q