Comments (11)
I'm going to close this issue because as I was working on other refresh problems with my SwiftUI layout (non Grid related), I learned more about Swift and found a better way of gathering the objects that I wanted to render from my Core Data object store. I had almost given up on Grid and was using LazyVGrid, but decided to give Grid another chance and low and behold, the refresh was working now without having to generate a random .id
for the grid. And it's refreshing just as fast as LazyVGrid now too.
Previously I was just looping through an array of Core Data objects directly, then calling a custom bind function to get and set the values. Now I build a Swift array of structs out of those Core Data objects and iterate over those. That seems to have done the trick.
from grid.
I tried turning off the cache, but that didn't work.
Grid(tracks: 3) {
// code
}.gridCache(.noCache)
from grid.
So is there no way to refresh the grid when new data arrives?
The only way I've been able to make it work is by tearing down my entire NSHostingController, removing the view, then re-creating the NSHostingController and the SwiftUIView that contains the Grid and sending the data into it again.
But it works fine with LazyVGrid, just not with this Grid component. It just always shows the first piece of data that was passed to it.
There must be a better way to refresh the grid when new data is sent to my SwiftUI view.
from grid.
So I noticed that the layout cache is for iOS only. I'm working on a Mac app at the moment. So the cache shouldn't affect me.
But why actually is the cache only for the iOS version? Just curious.
I'm still not understanding why I'm not seeing any new data appear on screen when I pass in new data. Yet with LazyV/HGrid, it does work to show new data.
from grid.
Ok, so I'm going to post my code that I'm working with. If anyone sees anything wrong with my approach I'd love to know what I'm doing wrong. Basically I have a list of objects I'm iterating over and depending on their types, I'm generating different kinds of views.
When I pass in new data into the ObservableSelection
, I do see that the code within the Grid block is getting executed and each of my views within the Switch
statement are being called. Except the actual body
of each of the individual views is never called (except when first displayed).
Maybe this has something to do with Grid, I'm not sure. All I know is if I change to LazyVGrid, it works. But I don't get the layout I want.
Any ideas?
struct RecordEditSwiftUIView: View {
let mainContext : NSManagedObjectContext
let databaseDocument : TFDatabaseDocument
private var fields : [CDField] = []
@State var selectedField: CDField?
@State private var dateModified : Date?
@ObservedObject private var selection : ObservableSelection
init(mainContext: NSManagedObjectContext, databaseDocument: TFDatabaseDocument, selection : ObservableSelection) {
self.selection = selection
self.mainContext = mainContext
self.databaseDocument = databaseDocument
self.fields = selection.form.sortTheFields() ?? []
}
var body: some View {
VStack {
if let values = selection.record.formattedValues {
Grid(tracks:3) {
ForEach(values, id: \.self) { value in
if let field : CDField = value.keys.first {
let fieldValue = value[field] ?? ""
switch field.fieldType {
case kFieldTypeNumber:
TextFieldView(record: selection.record, field: field, fieldValue: fieldValue, selectedField: $selectedField)
.gridItemAlignment(.topLeading)
.frame(maxHeight:70)
case kFieldTypePhoto:
PhotoFieldView(record: selection.record, field: field, selectedField: $selectedField)
.gridSpan(row: 3)
.gridItemAlignment(.topLeading)
case kFieldTypeNote:
let attributedValue : NSAttributedString? = selection.record.attributedValueFor(field: field)
NoteFieldView(record: selection.record, field: field, fieldValue: attributedValue ?? NSAttributedString(string: ""), selectedField: $selectedField)
.gridSpan(column: 3)
.gridItemAlignment(.topLeading)
.frame(maxHeight:200)
case kFieldTypeSection:
SectionFieldView(record: selection.record, field: field, selectedField: $selectedField)
.gridSpan(column: 3)
.gridItemAlignment(.topLeading)
.frame(maxHeight:40)
case kFieldTypeWebSite:
TextFieldView(record: selection.record, field: field, fieldValue: fieldValue, selectedField: $selectedField)
.gridItemAlignment(.topLeading)
.gridSpan(column: 2)
.frame(maxHeight:70)
default:
TextFieldView(record: selection.record, field: field, fieldValue: fieldValue, selectedField: $selectedField)
.gridItemAlignment(.topLeading)
.frame(maxHeight:70)
}
}
}
}.gridContentMode(.scroll)
} else {
Spacer()
Text("no selected record")
Spacer()
}
}.padding(.top)
.padding(.leading)
.padding(.trailing)
.frame(minWidth: 600, maxWidth:.infinity)
.onTapGesture {
selectedField = nil
}
.background(Color(NSColor.controlBackgroundColor))
}
}
from grid.
So I found that if I add .id(arc4random())
to the Grid view, then it does refresh when I pass in new data. But it's slow compared to using LazyVGrid. Quite a bit slower in fact.
Is there a better solution?
from grid.
Is this project dead though? Not one response. Hmm...
from grid.
Hey @brendand, no, the project isn't dead, but unfortunately we do not have time right now to address this issue. But rest assured we will most certainly address this once we find time. Have a nice day
from grid.
Hi @f3dm76 Thanks so much for your response. That's good to know. Using the random .id
does work, but it's slow, so hoping there's a better solution possible. Looking forward to seeing what you are eventually able to come up with. Thanks for creating this project. It's really great!
from grid.
Hi @brendand have you by any chance come across the removal of items from LazyVGrid?
from grid.
Hi @Medef I haven't done anything with this in a while actually. Still using Grid, but haven't actually looked at in in a while. I'll get back to it eventually once I'm finished with some updates to my underlying data model.
from grid.
Related Issues (20)
- Add Github Sponsor or other way to "tip" HOT 2
- Nested grid causes parent grid layout issues
- Performance issue when scroll has 1000 items and more HOT 3
- Example app not working on Apple M1 computers HOT 1
- when the item count is 0 it align to center HOT 1
- can not add views with if state of a @State boolean value HOT 2
- Change cell span dynamically HOT 1
- Items overlap HOT 4
- Only show one item when view appear, click to restore HOT 2
- Grid lines HOT 2
- Timetable
- zIndex for Grid Items HOT 3
- TextEditor with multiple columns? HOT 1
- ScrollView Nesting Issues. HOT 5
- Top alignment for cells HOT 6
- Urgent! - 1.4.2 build fails HOT 1
- How to create columns with different view heights? HOT 1
- Does it support tvOS? HOT 1
- can support this layout? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grid.