Swift Xcode TcpPrinterConnection Remains open

// Expert user has replied.
W William Mays 10 months 2 weeks ago
76 2 0

I have successfully implemented the Link OS iOS SDK into my Xcode project.

I created a bridging header for the .h files, and I am able to print successfully to our Zebra Printers

I am having an issue where extra tags are being printed. Explained:

  1. Connection is open with IP and port
  2. ZPL data is sent to printer, and tag prints successfully 
  3. Connection is closed

About 20% of the time, when the app is opened again to the main page, the zebra printer will open the tcp connection and print the same tag again. I am stumped and have no idea what is causing this. Any help is appreciated. I added a time stamp to print on the labels, when the extra tag is printed it's using the time stamp form the original tag printed. 

I have already tried the following but the issue is still happening:

  1. Set the TcpPrinterConnection object to nil after closing
  2. Wrapped the function in an autoreleasepool

Code:

autoreleasepool {
    var connection = TcpPrinterConnection.init(address: printerIP, andWithPort: 6101)
    
    if connection!.open() == true {
        do {
            try tool!.sendCommand(writeDate)
            connection!.close()
        } catch {
            connection!.close()
            print(error.localizedDescription)
        }
    }
}
Please register or login to post a reply

2 Replies

S Steven Si

Make sure the TCP connection open and its subsequent printing are done in a background thread. Secondly, tie the printing function with a UI event, such as a button press, etc. From what you described, it looks like the printing function is inadvertently launched when the UI is reopen. You should be able to trace the call flow in the debugger.

W William Mays

 I added the following code when the connection is opened and tag printed and I am getting the same issue. The function is only called when a button is pressed, so that is why I am confused on how it's being called. 

I am not able to trace the call flow as this only occurs 20% of the time, I have left my phone connected via Xcode and can't replicate it. 

DispatchQueue.global(qos: .userInitiated).async {
CONTACT
Can’t find what you’re looking for?