It seems the solution lies in using 'autoreleasepool'. If I wrap the code in updateFile with a autoreleasepool block, the memory buildup is reduced to a minimum, which is within what I expect is needed during the processing. In a small test I reduced the buildup from 243 MB to less than 7 MB, so I considder this solved.
Post
Replies
Boosts
Views
Activity
Please feel free to file the bug. I havent tried filing bugs yet and am a little pressed for time.
I figured that emptying the array would reset the scroll. Only problem was that setting the new data right away wasn't seen by the ScrollView as two changes. So I tried delaying setting the data and a small delay is apparently enough the the ScrollView to recognise the two changes.A bit of a hack in my own oppinion, it should work better and maybe this will be corrected in a future Xcode/SwiftUI release. Until then, this will work ok for me.Thanks for the guidance - again :-)
I found a workarround that seems to work, maybe not optimal or pretty. It needs more testing to verify that it works consistently.Instead of changing the data variable in one step like this:self.data = self.idx == 0 ? ContentView.data1 : ContentView.data2I first empty the array and then set it, but with a little delay. That way the ScrollView empties out, reset the scroll position (as it's empty) and repopulates with the new data and scrolled to the top.self.data = []
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01)
{
self.data = self.idx == 0 ? ContentView.data1 : ContentView.data2
}
I did a quick test, assuming that an initial offset for the scroll would be 0 (?) by adding the following line to the ScrollView:.content.offset(y:0)At the minimum, I would expect this to position the ScrollView at a specific position. But instead, it messes up the layout of the ScrollView and also disables the ability to scroll the view. Even though I could read out the current scroll position (which I've found several examples for similar to the linked post) I fail to see how that would help when I cannot push the position back without cripling the ScrollView.
I already found the stackoverflow post before coming here - and unfortunately it didn't help. The shown example do have two simple arrays and an index to change between them but this is just because it was as easy way of illustrating the issue.I my case, I have one single array which is populated dynamically from a database query. Switching between a couple of different ScrollViews is not realy an option.What I don't understand is why the contents change but the scroll offset doesn't.
Replacing List with VStack looses the scrolling, but wrapping it in a ScrollView takes care of that. Seems to work as I wanted. Strange that List doesn't allow this straight away.Thanks for the help
I can see the post wasn't clear enough :-) I want a different menu for each of the Text items. Also, I would like that only the Text item is hightlight when the contextmenu is displayed, not the entire row.I'm trying to implement a grid combining a List and HStack for each row. Each item in the grid should have it own context menu (or appear like that) as the contents of the menu depends on properties of the item in the grid
Thank you very much for pointing this out. It solved my problem. I didn't considder having an empty onTapGesture handler would have any impact on the scrolling, but it does. Maybe the common use is to have the tap handler and then add longtap as an additional gesture (makes sense).Thanks again :-)
I've put together a small example that show this issue:struct ContentView: View
{
let data = [
"Test 1","Test 2","Test 3","Test 4","Test 5",
"Test 6","Test 7","Test 8","Test 9","Test 10",
"Test 11","Test 12","Test 13","Test 14","Test 15",
"Test 16","Test 17","Test 18","Test 19","Test 20"
]
var body: some View
{
List
{
ForEach(data,id:\.self)
{
item in
Text(item).onLongPressGesture{}
}
}
}
}If I try to drag the list pressing on any text, the list wont move. If I remove the longpress handler, it moves no matter where I press down.