Post

Replies

Boosts

Views

Activity

Keep ScrollView position when adding items on the top
I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData. The list has one hundred items 1, 2, 3, ..., 100. The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen. Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position. Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down. Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
7
1
7.2k
Jun ’23
SwiftUI List and LazyVStack becomes jittering with large data
Based on the session video 'Loading and Displaying a Large Data Feed' I observed a performance problem when the number of displayed rows in a List or LazyVStack increases. Instead of an earthquake app lets think of a messaging app. New messages are fetched from a server, injest in a background task into a core data persistent store and displayed with a SwiftUI LazyVStack embedded in a scrollview. The rows in the list have different height. But this is not the problem because I wrote also a test app with a fixed content height for each row. Scrolling through a list of data retrieved by a @FetchRequest works pretty well. I passed the fetched items using a @ObservedObject to the SwiftUI row views so that property faults are only handled for the displayed rows. Row views have also an identifier set so that SwiftUI can reuse them. Everything works fine like in the earthquake example till to the point where the number of fetched items increases (approximate >2000). First when I scroll the list it is smooth but when I scroll more to the bottom items the jittering starts. I observed also that scrolling to the bottom is still smoother than when I scroll back to the begin of the list. Maybe because of the height calculation for the rows above. But this happens only when the number of items fetched from core data is very high. For only a few hundred items the earthquake approach works pretty well in this messaging app environment. Note:I used also request.includesPropertyValues = false as recommended by someone in the internet but this could not fix the jittering with large core data fetch results. Also switching from LazyVStack to List was not a solution for the jitter.
0
0
1.3k
Oct ’22
Code coverage in SwiftUI
Even when you separate business logic code from your SwiftUI views the previews and also view bodies seems both to count for the unit test code coverage. I read many articles in the internet about how to test SwiftUI code and archive good code coverage. But they all seem not to be the perfect solution for this task. Is there any good guide for unit and ui testing SwiftUI code that Apple recommends for their apps?
0
0
909
Jun ’22
How to deactivate SwiftUI toolbar animation
I have a simple SwiftUI List view with a .toolbar modifier containing three ToolbarItems. The placement is set to bottom. On the iPhone it seems as if the toolbar is animating from the bottom right corner whenever I launch the app. Having this view as a destination view it looks really strange. The NavigationLink causes the List view to appear from the right while the toolbar slides in from the bottom right corner. Is there a way to make the bottom toolbar not to slide it from the corner?
0
0
579
Jun ’22
Partial core data updates in Swift
Maybe I misunderstood the the 2019 WWDC video 'Making apps with core data'. Because when I use NSBatchInsertRequest together with NSMergeByPropertyObjectTrumpMergePolicy and unique constraints I can only add new records with the given id. Existing core data objects will not be partially updated. Example Entity: Person Attributes: FirstName = John LastName = Appleseed ID = 1234 (unique constraint) Goal in this example should be to change only the FirstName. As far as I understood the session video we can omit some attributes and call NSBatchInsertRequest with the above ID and change FirstName to 'Johnny'. But this seems not to work. Maybe someone has an idea if I am doing something wrong here or if this is really not possible to omit values and change single attribute values. I want to avoid fetching existing records, updating the values that should be changed and committing the changes back to the persistent store if there is maybe a better solution.
0
0
709
Dec ’21
Updates on SwiftUI list causes detail views to pop
I found this problem many times in the internet but not a solution for this. I have a SwiftUI list with data fetched from a server. The app uses a periodically background sync (into a core data background context). So the rows of this list can change. To make it simple to understand think of a message app like on the iPhone: struct MasterView: View {     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Chat.name, ascending: true)],         animation: .none)     private var chats: FetchedResults<Chat>     var body: some View {         List {             ForEach(chats, id: \.chatID) { chat in                 NavigationLink(destination: DetailView(chat: chat)) {                     Text(chat.name ?? "")                 }             }         }     } } The detail view is not so important here but keeping with the example of a message app you would have something like the following code to display the messages of a selected chat: struct DetailView: View {     let chat: Chat     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Message.timestamp, ascending: true)],         animation: .none)     private var messages: FetchedResults<Message>     var body: some View {         List {             ForEach(messages, id: \.messageID) { message in                 Text(message.messageText ?? "")             }         }         .onAppear {             if let chatID = chat.chatID {                 messages.nsPredicate = NSPredicate(format: "chatID == %@", chatID)             }         }     } } In the internet I saw also other examples without using core data for the master view. If the details view is visible to the user and the master view receives new data from the server the details view will always close (pops) automatically. This happens also when the chatID has not changed and still exists. A change of the master or also if in a split view the master view scrolls outside the visible area then the detail screen will close. As you see I specified the item ids in both ForEach lists and added also in some further tests identifiers to the NavigationLink or destination of the NavigationLink. But as soon the master list updates the old NavigationLink seems to get deallocated and therefore the detail views will close. Does someone know how to fix this? Of course I could add a button inside the masters list, store the selection and place the NavigationLink outside from the list item as I saw in one example, but it seems to be more a quick hack as a good solution.
0
0
660
Oct ’21
High battery drain using Xcode 13
I have two Mac Book Pro 16'' 2019. On both I see a very high battery drain using Xcode 13 while using the simulator. Even when the app is not running only the simulator is launched in the background the MacBook gets very hot and the battery drains empty very fast. For the beta this was ok. But with the RC I expected that this was only a temporary problem. Does some else has this problem also?
16
1
6.2k
Sep ’21
Search for max value coordinate in a MTLTexture
I struggle with the implementation of a compute kernel to solve a relative simple search problem. I want to find the coordinate with the highest pixel value within a MTLTexture. I have already an implementation where I copy the content of the MTLTexture into CPU memory and apply some min/max commands from the accelerate framework. But maybe there is also a simple solution for this task using Metal which I haven't found yet.
1
0
942
Jan ’21
Xcode app cannot see the driver anymore on Big Sur
My app uses an external PCI board connected with the thunderbolt cable to a 2019 MacBook Pro (Big Sur beta 10). The strange thing is that the external example apps coming with the driver can communicate with it. The same apps compiled with Xcode 12.2 beta cannot communicate with the driver even it is the same code. When I installed the driver together with the example apps a window appeared that these software come from an external source and the usage must be granted in the security settings. I did it and therefor the apps work fine. Is there maybe a similar setting now in Xcode where I must allow the compiled app to communicate with third party drivers? The code of the app was unchanged and worked with a previous macOS version.
3
0
941
Oct ’20
Metal Triple Buffering and Capture Scope
Can someone help me with this hopefully simple problem? I use triple buffering in my metal app similar to Apples Best Practice Guide. Three threads are simultaneously encoding rendering commands to command buffers enqueued in into one MTLCommandQueue. What is the best way to debug this scenario with the Xcode's frame debugger? I have some problems setting the capture scopes begin and end point in such a multithreaded environment to isolate the commands encoded from one thread.
1
0
909
Oct ’20
How to implement a Xcode style SwiftUI tabview
Above Xcode's navigator area or uitilty area there are some buttons for switching the content views below. I implemented the standard SwiftUI TabView and also a Picker with the segmented style to get the same appearance in my macOS app but both are looking different. Plain buttons in a HStack above my content area are solving the problem. But it seems more like a workaround. Is there maybe a simpler solution to implement a Xcode style tabview that I have overseen.
0
0
421
Oct ’20
How to add a label to a SwiftUI Textfield in macOS
Is there a way to add a label to SwiftUI Textfield inside a SwiftUI Form so that the label is aligned with the other controls? Placeholder in textfields will disappear when you enter values so this won't help much. All other controls have labels in macOS form with the same width so that their controls are correctly aligned. Textfield seems not to have this option. I tried several workaround like HStacks of Text and Textfields or custom alignment guides but without success.
2
1
4.1k
Oct ’20