Hi, I'm using ModelActor to avoid doing query operations in the main thread, so I'm doing them in concurrent threads. However, I'm having the same problem. I'm having a memory leak that I can't figure out why. Have you been able to solve the problem? Will it cause a problem when I use it like this? I'm thinking of uploading the app to the app store, will Apple cause a problem? Thank you very much. I would be very grateful if you share your experience.
Post
Replies
Boosts
Views
Activity
If you are going to make only a simple query, I recommend you to do it in MainView. However, if you are going to apply operations to your data, it may make sense to query in the View Model. According to the research I did, before querying, you need to pass the modelContext to the View Model and then use FetchDescriptor. If you search Query in View Model on the internet, you can get more detailed information. If you have any questions, please feel free to ask. I will be happy to help you.
If you are going to make only a simple query, I recommend you to do it in MainView. If you are going to apply other operations to your data, it may make sense to use a View Model. To make a query in a View Model, you must first pass the modelContext into it. After passing it, you should use FetchDescriptor to query. You can search what you want to do on the internet by saying Query in ViewModel. I tried to explain briefly. If you have any questions, please feel free to ask. I would be very happy to help.
When trying the app on my personal iPhone, cpu usage is max 50%. However, when I try it in the simulator, when I open the chart screen, it goes above 100% and goes back down to 0. If you stop the app in the meantime, you get the errors I mentioned. If you stop the application after the cpu usage drops to 0, you do not get any error. Is this a normal situation?
Using the @Query macro under the @Observable macro may not be a very correct use. In this case, I recommend you to change the structure of your code if you can. If you give more information about what you want to do, I will be happy to help :)
If I understand your problem correctly, it would be better to use @Bindable in this structure. More information about Bindable
Using @Bindable
@Bindable var stagione: Stagione
Let's make changes in Textfields and Button
TextField("Categoria", text: $stagione.categoria)
Button("Aggiorna dati") {
dismiss()
}
.disabled(!modifica)
By doing this, the values in the TextFields will be saved without any function. Please feel free to ask your question :)
İt's same for me. Any solution?
Thank you sooo much for your answer. That's completely working. I did it like that for design. (I mean else section). And I would be very grateful if you could explain the code. Because it's a little confusing. Thank you again :)
Picker("Marka", selection: $brandIndex) {
Text("Seçin").tag(0)
ForEach(cars.indices, id: \.self) {
Text(cars[$0].brand).tag($0 + 1)
}
}
.onChange(of: brandIndex) {
modelIndex = 0
}
if brandIndex != 0 && modelIndex <= cars[brandIndex - 1].models.count {
Picker("Model", selection: $modelIndex) {
Text("Seçin").tag(0)
ForEach(cars[brandIndex - 1].models.indices, id: \.self) {
Text(cars[brandIndex - 1].models[$0])
.tag($0 + 1)
}
}
} else { // This section for design
Picker("Model", selection: $modelIndex) {
Text("Seçin").tag(0)
}
}
Thank you for your answer @Claude31 . I'm trying to get the vehicle make and model from the user. I have a .json file which has all car brand and models. I'm getting values from viewModel and this is my struct:
struct Car: Decodable, Identifiable {
enum CodingKeys: CodingKey {
case brand
case models
}
var id = UUID()
var brand: String
var models: [String]
}
In the first picker I want to select brand. Then I want to see models according to the brand I chose.
Picker("Brand", selection: $brandIndex) {
Text("Choose").tag(0)
ForEach(cars.indices, id: \.self) {
Text(cars[$0].brand).tag($0 + 1)
}
}
Picker("Model", selection: $modelIndex) {
Text("Choose").tag(0)
if brandIndex != 0 {
ForEach(cars[brandIndex - 1].models.indices, id: \.self) {
Text(cars[brandIndex - 1].models[$0])
.tag($0 + 1)
}
}
}
First, I select a brand and model, then when I change the brand, if the new brand does not have that value, I get the error I mentioned above. I thought that this situation would be solved if the modelIndex variable was set to 0 every time the brand was changed. Thank you so much.
Thank you for your support. But how can I clear binding value?