Whenever a user returns to a form sub-view after saving data through a binding variable, the form displays old data from before it was edited, and I cannot understand why.
I have an array of Face objects (technically Core Data entities) declared as a State variable in a parent NavigationView. I then loop through them to create NavigationLinks like this:
#State var formDieFaces: [Face] = [] // this gets initialized akin to the below (truncated)
...
let face1 = Face(entity: Face.entity(), insertInto: nil)
self._formDieFaces = State(initialValue: [face1])
...
ForEach (formDieFaces.indices) { curIndex in
NavigationLink(
destination: FaceForm(self.$formDieFaces[curIndex]))
) {
FaceRowView(faceToList: self.formDieFaces[curIndex])
}
}
And in FaceForm, I receive the variable and "save" it as such:
#Binding var faceToEdit: Face
#State var formFaceText: String = String()
...
// in the form
TextField("Placeholder text", text: $formFaceText)
...
// on save, do the below
self.faceToEdit.text = self.formFaceText
What's weird is that on "saving" in FaceForm, the main NavigationView does update, and in that NavigationLink (FaceRowView) everything shows the correct data. But if I click back into FaceForm thereafter, it displays old data from before the save.
I have a feeling I'm doing something phenomenally obtuse with the State and Binding variables, but I just can't for the life of me figure out what's going on. I've tried various combinations of #ObservedObject, #ObservableObject, #StateObject, etc.
Could anyone please point me in the right direction?
Edit: to be clear, FaceRowView works and displays the correct data. FaceForm is where the problem is occurring, almost like it's remembering an old copy of the data.
If it's helpful, here is a link to a video of the behavior: https://youtu.be/8eC-TdtFP5s
For those curious, I'm not 100% sure this is a pure, proper SwiftUI solution, but I found a hack/workaround on this page that I've implemented and seems to have "solved" the problem: https://stackoverflow.com/a/57733708.
Related
The following code was written to display a single string (6000 chars) of environment values:
struct ContentView: View {
#Environment(\.description) var description
var body: some View {
VStack {
Text("#Environment Values").font(.title)
ScrollView {
Text("\(description)").font(.caption)
}.padding()
}
}
}
The result is inconsistent on both simulator and iPhone. When run, sometimes the information is displayed as desired on the left but often no values are displayed as shown on the right:
Other things tried didn't resolve the problem. Placing the environment variable in a viewmodel gives the message "Accessing Environment's value outside of being installed on a View. This will always read the default value and will not update."
Splitting the string into two strings gives the same inconsistent results.
Is this a thread issue?
Outside of listing every environment key and value, can anything be done to display the #Environment(.description) consistently?
When executing the .onDelete function, I am getting an index out of range. I have successfully used onDelete elsewhere in my app, deleting data directly from core data. This version is to only delete from a list not stored in core data.
I created some temporary lists for testing, and using this .onDelete was successful. The successful test cases were with arrays that already had data in them and did not need to be initialized. The array used in the code below is one that is initialized when the user lands on this page - could this be why it is failing?
There is lots of code inside this struct, but the relevant parts are below:
#State var recipeStep: [String]
More code, eventually reaching the ForEach causing the problems
List {
ForEach(0..<recipeStep.count, id: \.self) { index in
HStack {
Text(String(stepNumber[index]) + ".").bold()
TextField("", text: $recipeStep[index]).multilineTextAlignment(.leading).padding(.leading)
}
}.onDelete { (indexSet) in
recipeStep.remove(atOffsets: indexSet)}
Any thoughts as to why I am getting an index out of range error?
You loop with recipeStep in
ForEach(0..<recipeStep.count, id: \.self) { index in
While you use both $recipeStep[index] and stepNumber[index]
So recipeStep and stepNumber should be of same size to avoid crashes or better union them in 1 model
I was able to get this to work by modifying my textfield to work the following way:
EditorView(container: self.$recipeStep, index: index, text: recipeStep[index])
The solution was found here, posted by Asperi: https://stackoverflow.com/a/58911168/12299030
I had to modify his solution a bit to fit my forEach, but overall it works perfectly!
I'm working on a weather app where I have subviews for each statistic ex: CurrentWeather, HourlyForecast, and DailyForecast are all separate views
and I am currently making a separate api call for each view but I know that this is very inefficient and I was wondering how I could call the func only once for everything. I have tried putting the func in a .onAppear on the "home screen" of my app which features all of the subviews but it doesn't seen to work as the data I need is being accessed in the subviews.
Any help would be very much appreciated as I am fairly new to SwiftUI
Here is a version of what I am basically doing right now:
#EnvironmentObject var data: WeatherAPI
VStack {
// Weather Condition and Temp
CurrentWeather().environmentObject(WeatherAPI())
//Hourly Forecast
HourlyModuleView()
//Weekly Forecast
ForecastModuleView().environmentObject(WeatherAPI())
}.onAppear(perform: data.loadData)
You should remove over-defines for environment objects and use one from, as I understood, HomeScreen view, because environment object injected into root view becomes automatically available for all subviews, like
#EnvironmentObject var data: WeatherAPI
...
VStack {
// Weather Condition and Temp
CurrentWeather() // << data injected automatically
//Hourly Forecast
HourlyModuleView() // << data injected automatically
//Weekly Forecast
ForecastModuleView() // << data injected automatically
}.onAppear(perform: data.loadData)
I want to use a table for navigation purposes. Therefore I use the following code (the table looks good, all datas are complete):
// Admin.view.xml View
<Table id="objectsTable" itemPress="onSelectionChange">
// Admin.controller.js Controller
onSelectionChange: function(event) {
// After call undefined
var partnerId = event.getSource().getBindingContext().getProperty("BUSINESS_PARTNER_ID");
// After call defined and correct
var objectId = event.getSource().getBindingContext().getProperty("ID");
var router = this.getOwnerComponent()
.getRouter();
router.navTo("adminDetails", {
partner: partnerId,
object: objectId
});
After debugging I found out, that the value objectIdis undefined (while ID is present). This causes the navigation to not work properly.
So I looked at the data source (oData), which looks as follows :
ID | BUSINESS_PARTNER_ID | ADDRESS | FILES (oData association/navigation) |
.... All records are available, including the BUSINESS_PARTNER_ID
Why is the variable BUSINESS_PARTNER_IDundefined while all the data from the records are displayed correctly? I can query it, except BUSINESS_PARTNER_ID. Does anybody know how I can fix this?
Do partnerId and objectId have values? If the values for these are there then we need to check the routing and your manifest files. If partnerId and ObjectId are blank.
If these fields are blank i can think of another fix. Instead of binding the event from table, i believe you must have a columnlistitem or objectlistitem under your table. You can assign the press event on that and move this code to that.
Basically i am triggering the event from list item instead of table.
Thanks and Regards,
Veera
"Just" change my query to
event.getParameter("listItem").getBindingContext().getProperty("XYZ");
I understand how I could, for example look at one element in a table, but what's the correct way to simply iterate through all the elements, however many are found, to for example tap on them?
let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)
Okay I succeeded in figuring out the simple syntax. I have just started working with Swift and so it took me sleeping on it to think of the answer.
This code works:
var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}
print (elementLabels)
My guess is the answer is there is no way to do so.
The reason is because of my experience with one of the critical iOS components while making a number of UI tests: UIDatePicker.
If you record a test where you get the page up and then you spin the picker, you will notice that the commands are all non-specific and are screen related. In the case of the picker, however, community requests resulted in the addition of a method for doing tests: How to select a picker view item in an iOS UI test in Xcode?.
Maybe you can add a helper method to whatever controller contains this table. Also, keep in mind that you can easily add methods without polluting the class interface by defining them as extensions that are in test scope only.
For Xcode 11.2.1, SwiftUI and swift 5 based app, the following code works for testing a list, each element in this case appears as a button in the test code. The table is set up like this (for each row) :
NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
}
Then I catch the members of the table by getting the buttons into an array:
let myTable = app.tables.matching(identifier: "newTopicTable")
var elementLabels = [String]()
for i in 0..<myTable.buttons.count {
elementLabels.append (tablesQuery.buttons.element(boundBy: i).label)
}
print (elementLabels)
Finally, I deleted each member of the table by selecting the detail view where I have a delete button, again with
.accessibility(identifier: "deleteTopic"
I wanted to delete all members of the table:
for topicLabel in elementLabels {
let myButton = app.buttons[topicLabel]
myButton.firstMatch.tap()
app.buttons["deleteTopic"].tap()
}