Home > Software design >  Unable to get click event with observedObject in SwiftUI
Unable to get click event with observedObject in SwiftUI

Time:09-03

I have tried below code. However, I am unable to get click event in ObservedObject. Did I made any mistake.


struct ContentView: View {
    
    @StateObject var network = Network()
    
    var body: some View {
        VStack {
            SecondView(network: network)
            Text(self.network.networkObserver.sucess?.description ?? "Nil")
            
        }
    }      
}

SecondView Code:- Here is the code when I need to have click happened then revert to main content view.

public struct AdsView: View {
    
    @State private var banner: Model?
    @State private var image: UIImage?
    @State private var scale: Double = 1.0
    
    @ObservedObject var network: Network
    
 public var body: some View {
        Group {
            if let image = image {
                Text("AdSDK mockup. Click on image")
                
                Image(uiImage: image)
                    .resizable()
                    .scaledToFit()
                    .scaleEffect(scale)
                    .gesture (
                        TapGesture()
                            .onEnded { _ in
                                self.scale -= 0.1
                                network.networkObserver.sucess = network.networkObserver.sucess ?? false ? false : true
                            }
                        )
            } else {
                Rectangle()
                    .background(Color.red)
            }
        }
    }

Note:- Network class are in my custom library.


public class Network: ObservableObject {
    
    @Published public var adImage: UIImage?
    @Published public var networkObserver = NetworkObserver()
    
    public init() {
        
    }

    public func getImage(for imageURL: String) async throws { 

    }

}

And here is my ObservableObject


public class NetworkObserver {
    
    public var sucess: Bool?
    public var error: RequestError?
    
    public init() {
        
    }
    
}

If you need more information please let me know.

Thank you.

CodePudding user response:

As @workingdogsupportUkraine suggest in comment I need to change my NetworkObserver class to struct.

CodePudding user response:

Add this class somewhere in your code:

@MainActor class DelayedUpdater: ObservableObject {
    @Published var value = 0

    init() {
        for i in 1...10 {
            DispatchQueue.main.asyncAfter(deadline: .now()   Double(i)) {
                self.value  = 1
            }
        }
    }
}


To use that, we just need to add a @StatedObject property in ContentView, then show the value in our body, like this:



struct ContentView: View {
    @StateObject var updater = DelayedUpdater()

    var body: some View {
        Text("Value is: \(updater.value)")
    }
}



We can fix this by sending the change notifications manually using the objectWillChange property I mentioned earlier. This lets us send the change notification whenever we want, rather than relying on @Published to do it automatically.

Try changing the value property to this:

var value = 0 {
    willSet {
        objectWillChange.send()
    }
}
  • Related