简体   繁体   中英

How to delete all the cache or reload the application swiftui IOS CoreData

Hi I have an application which allows user to delete their account after they login in and went to the profile page. My application uses a viewModel to access the user details, the key concept is everytime the user login, the userViewModel will store the user object into a published variable, and that published variable is accessed every view in the application.

The problem is when user delete his account, the published user variable suddenlly becomes null and every view cannot access and caused a app crash. Is there anyway I can restart the application, enforce user back to login page without caching the other views or reload the application.

Thanks in advance.

Normally there would be something like this:

var body: some View {
    if let user = object.user {
        UserView(user: user)
    }
}

This way it is not possible to have a UserView without a user and thus it won't crash.

By the way, in SwiftUI there is @FetchRequest so you don't need to make your own view model object for fetching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM