Post

Replies

Boosts

Views

Activity

Reply to How to use network sockets with async/await?
Sorry, the question about async networking is now becoming a question about task cancellation. Feel free to throw me to new thread (pun intended). The bulk of the Swift concurrency runtime is in the Swift open source. I've got the swift repo and do look at it but not to that depth. Maybe one-day when I have lots of time :) I strongly recommend that you watch WWDC 2021 Session 10134 Explore structured concurrency in Swift. I did this, and learned a lot but not what I expected. I fully understand while a Task needs to complete and be able to be cancelled. What I fail to see, is how to opt-in. To try and explain, below is a simple example that works (assuming ostrich algorithm). Should the withCheckedContinuation be replaced with withTaskCancellationHandler? [1] Even if it is, how would I cancel a connection that is waiting for data to be received? Maybe I am starting to understand just enough to realise that something needs to change in the networking layer? [1] I found very little on withTaskCancellationHandler in documentation and online which may explain why it is not clear to me. import Foundation import Network class Udp { private var listner: NWListener private var receiveCallback: ((Data, NWEndpoint) -> Void)? func receive() async -> (Data, NWEndpoint) { await withCheckedContinuation { continuation in asyncReceive { data, endpoint in continuation.resume(returning: (data, endpoint)) } } } private func asyncReceive(callback: @escaping (Data, NWEndpoint) -> Void) { receiveCallback = callback } init(localPort: UInt16) throws { listner = try NWListener(using: .udp, on: NWEndpoint.Port(rawValue: localPort)!) listner.newConnectionHandler = { connection in connection.start(queue: .global()) self.receive(from: connection) } listner.start(queue: .global()) } func receive(from connection: NWConnection) { connection.receiveMessage { content, context, isComplete, error in if let rxData = content { if let receiveCallback = self.receiveCallback { receiveCallback(rxData, connection.endpoint) } } // Receive again: self.receive(from: connection) } } } print("This app listens for UDP on port 7000.") print("Use:") print(" nc -u 127.0.0.1 7000") print("to start sending data to me. I'll let you know when I've received it.") let udp = try! Udp(localPort: 7000) let (data, sender) = await udp.receive() print("Received data from \(sender)")
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’22
Reply to How to use network sockets with async/await?
Thanks, I did not consider the continuation solutions as my head was stuck on signalling. [As side, would be interesting to know how the continuations work under the hood as I have not come across that before]. Could you elaborate on the cancellation? Where would your example be cancelled (I assume before the continuation is called??). Would you be guaranteed that the continuation is not called? If so, then handling any context should be solvable. I am not clear where withTaskCancellationHandler(operation:onCancel:) comes in to the picture (where to prime it).
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’22
Reply to How to use network sockets with async/await?
I currently have a BSD socket where I use CFSocketCreate() with a receive callback. I've looked at NWConnection and have no problem switching to it if it makes more sense. My hardware will always use the same connection (same Source IP:Port to same Destination IP:Port). It will only change (source IP) for different hardware. From what I could gather, my problem for adopting async/await still has to do with signalling. I looked a bit at combine after posting (as it mentions networking) but have a feeling I was going down the wrong path. Ultimately, I need a await receive() somehow where it may block if it has not received anything yet and will not block if it has already received (and queued) data.
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’22
Reply to Why does custom Binding not update UI
Thanks. That is a step in the right direction and could work. Sorry for the piecewise info but I did not think it was relevant given the original question (I thought I just missed a notification in my Binding). There is a slight catch: This leaves me with two sources of truth. The bool value in the class may be changed by something other than the UI. I have a callback for when this happens, so I could notify/publish when it does. In the case of your example, I could use Binding instead of State so that the value could be changed externally as well, but that brings me back to an original concept that just felt clumsy. I could also add an Update button to my UI to refresh the values that may have changed externally, but again, it would be preferable to have just one source of truth.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’22
Reply to Why does custom Binding not update UI
I cannot change the code because It is in a 3rd party library for which I do not have the code. The other alternative would be to write a wrapper for the 3rd party class but that felt less elegant that the custom Binding. Seeing that the Binding works, I was wondering what it was missing and why it is not working. Is @Published adding some other functionality that could be simulated somehow.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’22
Reply to Does UITextField in UIViewRepresentable have a solution?
I found something that fixed my problem but I'm not sure it is the right way of doing things. Setting the horizontal content compression resistance priority to defaultLow made it scroll correctly. The makeUIView function below shows the full solution. func makeUIView(context: Context) -> UIViewType { // Setup text view: // ---------------- let textView = UITextField() textView.delegate = context.coordinator textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) // Creae a dummy view for the inputView (keyboard) so that the default // keyboard is not shown: let dummyView = UIView(frame: CGRect.zero) textView.inputView = dummyView return textView }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Cannot bind to local UDP socket - address already in use
Thanks. I am not sure what to do with this tread since the problem was not a code problem but a port being reserved by XQuartz. Thanks for the advice on netstat. I will use it in the future but it seems it would not have picked up the problem either since the port was not open, it was just being reserved. I cannot confirm since I've uninstalled XQuartz to resolve my issue (did not have the option to move other port). I am marking this as the answer purely to close the thread, and in case anyone ever gets to it. Let me know if you disagree.
Topic: App & System Services SubTopic: General Tags:
Feb ’22
Reply to How do I get recommended iOS app preview resolution?
OK, I knew I was missing something stupid: I should not be using iPhone 13 Pro for the video, but an iPhone 13 Pro Max. That gives the correct resolution. Bonus tip: Make use of iMovie's built-in support for generating app previews (yes, I know this should be obvious, but just in case). This did allow me to record my video on my iPhone 13 Pro and still save it in the correct resolution.
Jan ’22
Reply to Xcode 12.2 & Big Sur: Source editor extensions have vanished
The strangest thing now happened. The extensions that worked a day ago no longer work. They are not even there when I open System Preferences Extensions. It is as if it never existed. There was no reboot through the night, my macBook was closed. How can this be? Nothing that I tried (even reboot) is helping. I can even create a new project and follow the steps to create an extension but not even debugging works (it starts debugging but the extension is not under the Editor menu in xcode. Any suggestions?
Feb ’21
Reply to Xcode 12.2 & Big Sur: Source editor extensions have vanished
I can answer my own question about embedding and signing XcodeKit. I had my project selected instead of the target extension. Steps to link XcodeKit (just in case someone else needs exact steps): Select your top-level project in the Project Navigator. Under TARGETS, select your extension to see its settings. In the General tab, scroll down to Frameworks and Libraries. For a new extension, it will show two libs: 1) Cocoa.framework; 2) XcodeKit.framework. Both have the option Do Not Embed selected. Change XcodeKit.framework to Embed & Sign. Rebuild. I can also confirm this resolved the issue.
Feb ’21