0

I a having an issue with my button "Get New Connection Pin" It seem to be frozen or stuck at times. It's as it's not responding to being pressed. I have to keep pressing the button a few times before it will actually response. Some times its works and some times it does not. I am new to SwiftUI and not sure what I am doing wrong.

I notice that when I remove the styling from the button text and the button everything works great as I intended however when I add the styling I have issue with it responding

//
//  MainView.swift
//  Remote Bridge
//
//  Created by Jerry Seigle on 6/19/23.
//

import SwiftUI

struct MainView: View {
    // TODO: our app targets macOS 10.15. Update to @StateObject when targeting macOS 12 or later
    @ObservedObject private var viewModel = MainViewModel()
    @ObservedObject private var accountViewModel = AccountTabViewModel()
    
    var body: some View {
        VStack(spacing: 20) {
            Spacer()
            
            VStack(alignment: .leading, spacing: 10) {
                Text("Connection Pin:")
                    .font(.headline)
                    .foregroundColor(.gray)
                
                Button(action: {
                    viewModel.copyToClipboard(text: viewModel.roomIdString)
                }) {
                    HStack {
                        Text(viewModel.roomIdString)
                            .fontWeight(.semibold)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        
                        Image("doc.on.doc.fill")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 16, height: 16)
                            .foregroundColor(.blue)
                    }
                }
                .cornerRadius(8)
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding()
            .background(Color.gray.opacity(0.2))
            .cornerRadius(8)
            
            Button(action: viewModel.getRoomId) {
                Text("Get New Connection Pin")
                    .frame(maxWidth: .infinity, minHeight: 40)
                    .background(Color.blue)
                    .cornerRadius(4)
                    .contentShape(Rectangle()) // Make the entire button tappable
            }
            .buttonStyle(PlainButtonStyle())
            
            if !accountViewModel.isSubscriptionActive {
                Button(action: viewModel.upgradeForProAccessTap) {
                    Text("Upgrade for Pro Access!")
                        .font(.system(size: 14, weight: .regular, design: .default).italic())
                        .foregroundColor(.orange)
                }
                .frame(maxWidth: .infinity, alignment: .leading)
                .buttonStyle(PlainButtonStyle())
            }
          
            Spacer()
        }
        .padding()
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .onAppear(perform: viewModel.loadSavedRoomId)
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
            .frame(width: 225, height: 180)
            .background(Color.white.opacity(0.1))
    }
}

I am referring to this button

Button(action: viewModel.getRoomId) {
                Text("Get New Connection Pin")
                    .frame(maxWidth: .infinity, minHeight: 40)
                    .background(Color.blue)
                    .cornerRadius(4)
                    .contentShape(Rectangle()) // Make the entire button tappable
            }
            .buttonStyle(PlainButtonStyle())

it seems as it start to get stuck when I add .buttonStyle(PlainButtonStyle())

  • Sounds like something is blocking main thread for a long time. Pause your app when the button is frozen and check what each thread is doing. Either that, or something is overlapping with your button's touchable area – timbre timbre Jul 07 '23 at 17:51
  • Were do I go to pause app and where do I go to check to see what each thread is doing. Sorry I just started coding in Xcode and swiftui – Pastor J.G Seigle Jul 07 '23 at 17:52
  • You shouldn't be using `@ObservedObject` where you own the object. Use `@StateObject` instead – jnpdx Jul 07 '23 at 17:55
  • I am targeting macOS 10.15 when I try to use @StateObject I get error that it is only supported in macOS 11 and newer – Pastor J.G Seigle Jul 07 '23 at 17:59
  • Ah, okay -- are you aware that the models will be re-instantiated every time the view re-renders? You'll need to do some clever hacks to avoid issues with that. – jnpdx Jul 07 '23 at 18:02
  • Yes just learned about it. I could not figure out why some of function were re-instantiated. I fixed that issue with if statement check. Not clean but only way I know to fix it. I am so new to swiftUI so just learning. Still having issue not understanding why the button freezes when added the ```.buttonStyle(PlainButtonStyle())``` to the button – Pastor J.G Seigle Jul 07 '23 at 18:06
  • @PastorJ.GSeigle Debug > Pause will pause the app, the threads will appear in Debug Navigator. Check this answer for more details: https://stackoverflow.com/a/47324273/5318223. What you want to do is first of all check "Thread 1". It's a UI thread and it should only do UI work. So you can see if it's some other operation that is making it busy, or some UI is actually so heavy that it takes a while. Also pay attention to Debug Area log - it may show some "recoverable" exceptions, errors, or warnings wich can be relevant – timbre timbre Jul 08 '23 at 19:14

0 Answers0