Have you encountered any solutions to address the issue of the screen freezing and excessive CPU usage when attempting to implement the "Request Review" feature in your project? Specifically, I'm facing this problem each time I switch from a screen containing the @Environment(\.requestReview) property to another screen using @ObservedObject.

I've also noticed that the _requestReview changed message is printed multiple times without any apparent reason, as shown in the screenshot below.

Consol showing changed

Do you have any suggestions or insights on how to troubleshoot and resolve this issue effectively?

here is a project to replicate the issue

https://github.com/byaruhaf/DefTest

or you can use the code below

import SwiftUI
import Foundation
import StoreKit

struct ContentView: View {
    var body: some View {
        let _ = Self._printChanges()
        VStack(spacing: 40) {
            Image(systemName: "soccerball")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Football School")
            NavigationLink {
                StudentListView()
            } label: {
                Text("Click for Student List")
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}

struct StudentListView: View {
    @Environment(\.requestReview) private var requestReview
    var body: some View {
        let _ = Self._printChanges()
        VStack(spacing: 40) {
            Text("Student List")
                NavigationLink {
                    StudentDetailView()
                } label: {
                    Text("Click for Student Detail")
                }
                .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
final class Test: ObservableObject {}

struct StudentDetailView: View {
    @ObservedObject var test = Test()

    var body: some View {
        let _ = Self._printChanges()
        VStack(spacing: 40) {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

High CPU

High CPU

Increasing Memory

Increasing Memory

2

There are 2 best solutions below

0
On BEST ANSWER

issue Fixed in iOS 17.4 (FB13005087)

2
On

i am not able to comment posting answer. solution is already given in comment by @jnpdx

just change your ObservedObject to StateObject

like this

@ObservedObject var test = Test()

to

@StateObject var test = Test()

result what i get

enter image description here

@ObservedObject:

When you use the @ObservedObject property wrapper, the observed object's lifecycle is not tied to the view's lifecycle. The observed object can live beyond the view's scope and will only be deallocated when there are no more references to it. This can lead to situations where the observed object might outlive the view, potentially causing unexpected behavior or memory leaks if not managed properly.

@StateObject:

On the other hand, when you use the @StateObject property wrapper, SwiftUI takes care of managing the lifecycle of the object. The object is created and owned by the view, and it will be deallocated when the view itself is deallocated. This ensures that the observed object's lifecycle is tightly linked to the view's lifecycle, which can help prevent memory leaks and unexpected behavior.