I'd like to implement a basic Map view that will center on the users location when they tap a button, similar to the Apple Maps app. I tried the following, but whenever I tap the button, [SwiftUI] Modifying state during view update, this will cause undefined behavior. is printed in the console. It seems to me that updating the tracking state variable is causing the error. However, I'm not sure how else the state variable is meant to be used. The app does behave as intended despite printing the error. Does anyone have any experience with this or know what might be wrong?
struct ContentView: View {
#State var region: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 47.3769, longitude: 8.5417), latitudinalMeters: 2000, longitudinalMeters: 2000)
#State var tracking = MapUserTrackingMode.follow
var body: some View {
ZStack {
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: $tracking)
.ignoresSafeArea()
.task {
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization();
}
Button {
tracking = .follow
} label: {
Image(systemName: tracking == .follow ? "location.fill" : "location")
.padding()
}
.background(.white)
}
}
}
Seems to me it's a bug in Map (as of Xcode Version 13.3.1 (13E500a) and iPhone 13 Simulator). If you switch to the Breakpoints side-bar and click the + and add an All Runtime Issues breakpoint, if you debug and click the button you'll hit the breakpoint and see this:
This trace shows that when the button is tapped to change the tracking state, SwiftUI updates MKMapView with the new state by calling _setUserTrackingMode (line 13) but a side effect of this is a callback to mapLayerDidChangeVisibleRegion (line 9) and it tries to set the value of a Binding (line 6), most likely the coordinateRegion. It shouldn't be setting a Binding while it is updating the MKMapView from the State, which is what results in the warning. We should all report the bug - I submitted it as FB9990674 under Developer Tools - SwiftUI, feel free to reference my number.
I started to have a similar issue when moving to iOS 16 (Xcode 14.1).
In my case I had the following:
// LocationView
struct LocationView : View {
#StateObject private var viewModel : ViewModel
var body: some View {
Map(coordinateRegion: $viewModel.region)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
// View Model
#MainActor final class ViewModel : ObservableObject {
...
#Published var region : MKCoordinateRegion
...
}
What I did in this case to remove the warning was to create a Binding in my View to the Published property in the viewModel:
private var region : Binding<MKCoordinateRegion> {
Binding {
viewModel.region
} set: { region in
DispatchQueue.main.async {
viewModel.region = region
}
}
}
And then change the view to use this binding instead of the published variable in the viewModel:
Map(coordinateRegion: region)
The application keeps working as expected and the error/warning is no longer showing up.
Related
Simple sample code with toggle button (slightly modified from hackingwithswift:
This code(hackingwithswift original and my version) IS redrawing every list cell whenever any toggle happens. I modified code to better debug view drawing.
import SwiftUI
struct User: Identifiable {
let id = UUID()
var name: String
var isContacted = false
}
struct ProfileView: View {
#State private var users = [
User(name: "Taylor"),
User(name: "Justin"),
User(name: "Adele")
]
var body: some View {
let _ = Self._printChanges()
List($users) { $user in
ProfileCell(user: $user)
}
}
}
struct ProfileCell: View{
#Binding var user: User
var body: some View{
let _ = Self._printChanges()
Text(user.name)
Spacer()
Toggle("User has been contacted", isOn: $user.isContacted)
.labelsHidden()
}
}
Running app and toggling will print following in console for every toggle:
ProfileView: _users changed.
ProfileCell: #self, _user changed.
ProfileCell: #self, _user changed.
ProfileCell: #self, _user changed.
Hackingwithswift tutorial states "Using a binding in this way is the most efficient way of modifying the list, because it won’t cause the entire view to reload when only a single item changes.", however that does not seem to be true.
Is it possible to redraw only item that was changed?
Theoretically it should be working, but it seems they changed something since first introduction, because now on state change they recreate(!) bindings (all of them), so automatic view changes handler interpret that as view update (binding is a property after all).
A possible workaround for this is to help rendering engine and check view equitability manually.
Tested with Xcode 13.4 / iOS 15.5
Main parts:
// 1
List($users) { $user in
EquatableView(content: ProfileCell(user: $user)) // << here !!
}
// 2
struct ProfileCell: View, Equatable {
static func == (lhs: ProfileCell, rhs: ProfileCell) -> Bool {
lhs.user == rhs.user
}
// ...
// 3
struct User: Identifiable, Equatable {
Test module is here
SwiftUI n00b here. I'm trying some very simple navigation using NavigationView and NavigationLink. In the sample below, I've isolated to a 3 level nav. The 1st level is just a link to the 2nd, the 2nd to the 3rd, and the 3rd level is a text input box.
In the 2nd level view builder, I have a
private let timer = Timer.publish(every: 2, on: .main, in: .common)
and when I navigate to the 3rd level, as soon as I start typing into the text box, I get navigated back to the 2nd level.
Why?
A likely clue that I don't understand. The print(Self._printChanges()) in the 2nd level shows
NavLevel2: #self changed.
immediately when I start typing into the 3rd level text box.
When I remove this timer declaration, the problem goes away. Alternatively, when I modify the #EnvironmentObject I'm using in the 3rd level to just be #State, the problem goes away.
So trying to understand what's going on here, if this is a bug, and if it's not a bug, why does it behave this way.
Here's the full ContentView building code that repos this
import SwiftUI
class AuthDataModel: ObservableObject {
#Published var someValue: String = ""
}
struct NavLevel3: View {
#EnvironmentObject var model: AuthDataModel
var body: some View {
print(Self._printChanges())
return TextField("Level 3: Type Something", text: $model.someValue)
// Replacing above with this fixes everything, even when the
// below timer is still in place.
// (put this decl instead of #EnvironmentObject above
// #State var fff: String = ""
// )
// return TextField("Level 3: Type Something", text: $fff)
}
}
struct NavLevel2: View {
// LOOK HERE!!!! Removing this declaration fixes everything.
private let timer = Timer.publish(every: 2, on: .main, in: .common)
var body: some View {
print(Self._printChanges())
return NavigationLink(
destination: NavLevel3()
) { Text("Level 2") }
}
}
struct ContentView: View {
#StateObject private var model = AuthDataModel()
var body: some View {
print(Self._printChanges())
return NavigationView {
NavigationLink(destination: NavLevel2())
{
Text("Level 1")
}
}
.environmentObject(model)
}
}
First, if you remove #StateObject from model declaration in ContentView, it will work.
You should not set the whole model as a State for the root view.
If you do, on each change of any published property, your whole hierarchy will be reconstructed. You will agree that if you type changes in the text field, you don't want the complete UI to rebuild at each letter.
Now, about the behaviour you describe, that's weird.
Given what's said above, it looks like when you type, the whole view is reconstructed, as expected since your model is a #State object, but reconstruction is broken by this unmanaged timer.. I have no real clue to explain it, but I have a rule to avoid it ;)
Rule:
You should not make timers in view builders. Remember swiftUI views are builders and not 'views' as we used to represent before. The concrete view object is returned by the 'body' function.
If you put a break on timer creation, you will notice your timer is called as soon as the root view is displayed. ( from NavigationLink(destination: NavLevel2())
That's probably not what you expect.
If you move your timer creation in the body, it will work, because the timer is then created when the view is created.
var body: some View {
var timer = Timer.publish(every: 2, on: .main, in: .common)
print(Self._printChanges())
return NavigationLink(
destination: NavLevel3()
) { Text("Level 2") }
}
However, it is usually not the right way neither.
You should create the timer:
in the .appear handler, keep the reference,
and cancel the timer in .disappear handler.
in a .task handler that is reserved for asynchronous tasks.
I personally only declare wrapped values ( #State, #Binding, .. ) in view builders structs, or very simple primitives variables ( Bool, Int, .. ) that I use as conditions when building the view.
I keep all functional stuffs in the body or in handlers.
To stop going back to the previous view when you type in the TextField add .navigationViewStyle(.stack) to the NavigationView
in ContentView.
Here is the code I used to test my answer:
import SwiftUI
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
#StateObject var model = AuthDataModel()
var body: some View {
NavigationView {
NavigationLink(destination: NavLevel2()){
Text("Level 1")
}
}.navigationViewStyle(.stack) // <--- here the important bit
.environmentObject(model)
}
}
class AuthDataModel: ObservableObject {
#Published var someValue: String = ""
}
struct NavLevel3: View {
#EnvironmentObject var model: AuthDataModel
var body: some View {
TextField("Level 3: Type Something", text: $model.someValue)
}
}
struct NavLevel2: View {
#EnvironmentObject var model: AuthDataModel
#State var tickCount: Int = 0 // <-- for testing
private let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()
var body: some View {
NavigationLink(destination: NavLevel3()) {
Text("Level 2 tick: \(tickCount)")
}
.onReceive(timer) { val in // <-- for testing
tickCount += 1
}
}
}
I am trying to use the new App protocol for a new SwiftUI App and I need to detect a scenePhase change into .background at App level in order to persist little App data into a .plist file. I don't know if it is a bug or I am doing something wrong but it doesn't work as expected. As soon as a button is tapped, scenePhase change to .background when the scene is still active! In order to show an example of this weird behaviour, I am showing this simple code:
class DataModel: ObservableObject {
#Published var count = 0
}
#main
struct TestAppProtocolApp: App {
#Environment(\.scenePhase) private var scenePhase
#StateObject private var model: DataModel = DataModel()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(model)
}
.onChange(of: scenePhase) { newScenePhase in
switch newScenePhase {
case .active:
print("Scene is active.")
case .inactive:
print("Scene is inactive.")
case .background:
print("Scene is in the background.")
#unknown default:
print("Scene is in an unknown state.")
}
}
}
}
struct ContentView: View {
#EnvironmentObject var model: DataModel
var body: some View {
VStack {
Button(action: { model.count += 1 }) {
Text("Increment")
}
.padding()
Text("\(model.count)")
}
}
}
When the increment button is tapped, scenePhase changes to .background and then, when the App is really sent to background, scenePhase is not changed.
I found out that moving the .onChange(of: scenePhase) to the View (ContentView) works fine as I expect but Apple announced you can monitor any scenePhase change at App level and this is what I really want not at View level.
I also had a similar issue with scenePhase not working at all, then it worked but not as expected. Try removing only "#StateObject private" from your property and you will probably have other results. I hope new betas will fix this.
By the way, the recommended way of persisting little App-wide data in SwiftUI 2+ is through #AppStorage property wrapper which itself rests on UserDefaults. Here is how we can detect the first launch and toggle the flag:
struct MainView: View {
#AppStorage("isFirstLaunch") var isFirstLaunch: Bool = true
var body: some View {
Text("Hello, world!")
.sheet(isPresented: $isFirstLaunch, onDismiss: { isFirstLaunch = false }) {
Text("This is the first time app is launched. The sheet will not be shown again once dismissed.")
}
}
}
Xcode12 beta 6 seems to solve the issue. Now it works as expected.
Can someone point me in the right direction on how to implement a simple list with a confirm remove-function or at least show best practice here.
Below code will work if the alert-part is removed and delete action is immediate.
Somehow, the presentation of the confirmation-alert makes the the deletion act on the wrong list of persons. First removal also gives a console warning:
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.
However, I have no idea on how to solve this without removing the alert. By the way, this exact code worked a couple of weeks ago before my mac updated xcode i believe.
import Foundation
import SwiftUI
import Combine
struct Person: Identifiable{
var id: Int
var name: String
init(id: Int, name: String){
self.id = id
self.name = name
}
}
class People: ObservableObject{
#Published var people: [Person]
init(){
self.people = [
Person(id: 1, name:"One"),
Person(id: 2, name:"Two"),
Person(id: 3, name:"Three"),
Person(id: 4, name:"Four")]
}
}
struct ContentView: View {
#ObservedObject var mypeople: People = People()
#State private var showConfirm = false
#State private var idx = 0
func setDeletIndex(at idxs:IndexSet) {
self.showConfirm = true
self.idx = idxs.first!
}
func delete() {
self.mypeople.people.remove(at: idx)
}
var body: some View {
VStack {
List {
Text("Currently \(mypeople.people.count) persons").font(.footnote)
.alert(isPresented: $showConfirm) {
Alert(title: Text("Delete"), message: Text("Sure?"),
primaryButton: .cancel(),
secondaryButton: .destructive(Text("Delete")) {
self.delete()
})
}
ForEach(mypeople.people){ person in
Text("\(person.name)")
}.onDelete { self.setDeletIndex(at: $0) }
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The problem is due to conflict of updating alert closing & List record removing. The working workaround is to delay deleting, as below (tested with Xcode 11.4)
Text("Currently \(mypeople.people.count) persons").font(.footnote)
.alert(isPresented: $showConfirm) {
Alert(title: Text("Delete"), message: Text("Sure?"),
primaryButton: .cancel(),
secondaryButton: .destructive(Text("Delete")) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { // here !!
self.delete()
}
})
}
I've got a strange crash in SwiftUI / Xcode 11 beta 3 with code like the one below (I've kept only the bare minimum to show the behavior):
import SwiftUI
import Combine
final class AppData: BindableObject {
let didChange = PassthroughSubject<AppData, Never>()
init() { }
}
struct ContentView : View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView() ) {
Text("link")
}
}
}
}
struct DetailView : View {
#EnvironmentObject var appData: AppData
// #ObjectBinding var appData = AppData() -> Works
var body: some View {
List {
Text("A")
Text("B")
Text("C")
}
}
}
The BindableObject is injected in SceneDelegate.swift like this:
....
// Use a UIHostingController as window root view controller
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView()
.environmentObject(AppData()))
self.window = window
window.makeKeyAndVisible()
}
....
When following the NavigationLink it crashes with
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
If I remove the List view from the detail view it works OK. The same if I use #ObjectBinding instead (like in the commented line in my code).
The same code used to work in previous betas.
It's a bug in Xcode 11 beta 3. The old behaviour will likely return.
From https://developer.apple.com/tutorials/swiftui/handling-user-input as of July 4th 2019:
Step 4
In Xcode 11 beta 3, the LandmarkDetail view doesn’t automatically access the UserData object in the view hierarchy’s environment. The workaround for this is to add the environmentObject(_:) modifier to the LandmarkDetail view.
I think that this is by design. When you create DetailView(), it is disconnected from the hierarchy, and hence it does not inherit the same environment.
If you change your ContentView to the following, it won't crash. I think I remember having a similar problem with modals:
struct ContentView : View {
#EnvironmentObject var appData: AppData
var body: some View {
NavigationView {
NavigationLink(destination: DetailView().environmentObject(appData) ) {
Text("link")
}
}
}
}