Post

Replies

Boosts

Views

Activity

Reply to Swift - Async calls in loop
the loop basically "pauses" until I fetch every detail information of the iterated object, add it into the dataSource Array and then continues with the next one Do you really need the "pauses"? If your dataAccessService accepts multiple API calls simultaneously, you can send multiple requests simultaneously and construct the result when the last data is received.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Is there a way to use SIMD in Swift for better performance? (Question includes test code).
The SIMD average time is around 85% of the regular average - is that about what would be expected? It depends on many things. In your case, you use 2d vector operation which executes 2 scalar operations simultaneously. So, in an ideal best efficiency, you can expect 50%, are you OK with this? I too expected a little better than 85%, but I do not think it's too bad. With detailed examination of the generated codes, it could be a little better. But I recommend you to explore other Accelerate features if you want to improve the performance significantly,
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Swift - Async calls in loop
I just don't know how to construct the result with each response of the sent requests. Thanks for clarification. Then using DispatchGroup may be a good choice. Two more things to clarify: Does your dataAccessService calls completionHandler always in the main thread? (To make your code thread-safe, this is a big issue.) What happens in case of communication error in fetchUserById? (To use DispatchGroup properly, you cannot ignore error handling.)
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Opening an NSViewController as a Sheet
It is not clear how sheets of macOS are managed in SwiftUI. In my test project (exactly the same code as shown), the sheet had a size 700 x 200. I'm not sure if this works for you, but you can try setting the content size of the sheet window. 		override func viewWillAppear() { 				super.viewWillAppear() 				self.view.window?.setContentSize(CGSize(width: 200, height: 150)) 				 				messageLabel.stringValue = message 		}
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Sort list alphabetically
@Konste4sfi want to sort it with Sections from a to z in a list You need to create a structure which represents sections, and rows in each section. A simplified example: import SwiftUI struct ContentView: View { 		var inputArray: [String] = [ 				"Andrew", 				"Adam", 				"David" 		] 		@State var groupedArray: [String: [String]] = [:] 		var body: some View { 				List { 						ForEach(groupedArray.keys.sorted(), id: \.self) {key in 								Section(header: Text(key)) { 										ForEach(groupedArray[key]!, id: \.self) {value in 												Text(value) 										} 								} 						} 				} 				.onAppear { 						groupedArray = Dictionary( 								grouping: inputArray, 								by: {$0.first?.uppercased() ?? ""} 						).mapValues{$0.sorted()} 				} 		} } You may need to modify many parts of this code, depending on the details of your requirement.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Swift - Async calls in loop
My fetchUserById method looks like this Thanks for showing your code. That resolves the second question I have shown. What happens in case of communication error in fetchUserById? Seems your fetchUserById just shows some debug message and never calls completion handler on error. That's a very bad design when working with DispatchGroup. Can you change the method as to call completion handler in case of error? And another question is not answered. Does the method getDocuments(completion:) calls completion in the main thread?
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Opening an NSViewController as a Sheet
Do you happen to know what I need to do? Your way of closing the sheet looks a little awkward to me, so tried something like this and works in my environment. class SheetViewController: NSViewController { &#9;&#9;// MARK: - &#9;&#9;var message = String() &#9;&#9;var isPresented: Binding<Bool> = .constant(false) &#9;&#9; &#9;&#9;// MARK: - IBOutlet &#9;&#9;@IBOutlet weak var messageLabel: NSTextField! &#9;&#9;// MARK: - IBAction &#9;&#9;@IBAction func closeClicked(_ sender: NSButton) { &#9;&#9;&#9;&#9;/* closing window */ &#9;&#9;&#9;&#9;isPresented.wrappedValue = false &#9;&#9;} &#9;&#9;//... } struct SheetViewControllerRepresentation: NSViewControllerRepresentable { &#9;&#9;var message = String() &#9;&#9;@Binding var isPresented: Bool &#9;&#9;func makeNSViewController(context: NSViewControllerRepresentableContext<SheetViewControllerRepresentation>) -> SheetViewController { &#9;&#9;&#9;&#9;let mainStoryboard = NSStoryboard(name: "Main", bundle: nil) &#9;&#9;&#9;&#9;let sheetViewController = mainStoryboard.instantiateController(withIdentifier: "SheetView") as! SheetViewController &#9;&#9;&#9;&#9;sheetViewController.message = self.message &#9;&#9;&#9;&#9;sheetViewController.isPresented = _isPresented //<- &#9;&#9;&#9;&#9;return sheetViewController &#9;&#9;} &#9;&#9;//... } And use it as: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $sheetPresented) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;SheetViewControllerRepresentation(message: String(selectionIndex), &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;isPresented: self.$sheetPresented) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} Happy all your coming days.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to I'm trying to use two timers in the same SwiftUI view
I tried your code and found the closure for onReceive(timer2) was not invoked when the time intervals are different. You should better try to explain what your works means. As far as I tried, it seems autoconnect() cannot handle multiple timers properly. Please try something like this: struct ContentView: View { &#9;&#9;@State var isPlaying: Bool = true &#9;&#9;@State var timeRemaining: Int = 1800 &#9;&#9;@State var count = 1 &#9;&#9;@State var count2 = 10 &#9;&#9;let timer2 = Timer.publish(every: 1, on: .main, in: .default) &#9;&#9;let timer = Timer.publish(every: 0.5, on: .main, in: .default) &#9;&#9;init() { &#9;&#9;&#9;&#9;_ = timer.connect() &#9;&#9;&#9;&#9;_ = timer2.connect() &#9;&#9;} &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;Text("\(count)&#9;&#9;\(count2)") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.padding() &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onReceive(timer2) { _ in &#9;&#9;&#9;&#9;&#9;&#9;print("timer2") &#9;&#9;&#9;&#9;&#9;&#9;if self.timeRemaining > 0 && isPlaying == true { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.count2 += 1 &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onReceive(timer) { _ in &#9;&#9;&#9;&#9;&#9;&#9;print("timer") &#9;&#9;&#9;&#9;&#9;&#9;if self.timeRemaining > 0 && isPlaying == true { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.count += 1 &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to How to pass data from a TextField to a List (SwiftUI)
CardRow is just a View which cannot be a storage for showing a List. In detail, in which way do you want to store the passed values? Extend Card and add properties for the values Add another @State array(s) to CardsView Define a ObservableObject holding the values and use it as @ObservedObject in CardView Some other way
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Swift - Async calls in loop
From my understanding I'm afraid your understanding is not enough to explain the behavior of your code. Whether the completion handler is called in the main thread depends on the inner most async method getDocument. Have you defined the method by yourself? Or is it provided by some third party framework? If the latter, you need to check the documentation of the framework.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to No ObservableOblect
Added error details and error locations. Thanks for adding the info. But I cannot reproduce the error with your shown code. You should better check other parts of your code using Home.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20