Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not always persisting #9

Open
sbdchd opened this issue Jan 11, 2025 · 2 comments
Open

Not always persisting #9

sbdchd opened this issue Jan 11, 2025 · 2 comments

Comments

@sbdchd
Copy link
Contributor

sbdchd commented Jan 11, 2025

I can't reproduce on the simulator, but on my phone I'm running into a weird issue where the data doesn't persist when I force close the app.

My setup is:

// AppStorage.swift
import Foundation
import TinyStorage

enum AppStorageKey: String, TinyStorageKey {
  case authUserMeta
  case lastFullStorySync
}

extension TinyStorage {
  static let appGroup: TinyStorage = {
    let containerURL = FileManager.default.containerURL(
      forSecurityApplicationGroupIdentifier: Bundle.main.bundleIdentifier!.lowercased())!
    return .init(insideDirectory: containerURL, name: "tiny-storage-general-prefs")
  }()
}
struct SettingsView: View {
  @State var showSignIn = false

  @TinyStorageItem(AppStorageKey.authUserMeta, storage: .appGroup)
  var userInfo = AuthUserMeta(userName: "", createdAt: nil, karma: nil)

  var body: some View {
    VStack {
      Form {
        Section {
          if !userInfo.userName.isEmpty {
            NavigationLink(value: ListDestination.settingsUser) {
              UserProfileMetaView(userName: userInfo.userName, createdAt: userInfo.createdAt)
            }
          } else {
            Button(
              action: {
                showSignIn = true
              },
              label: {
                  Text("Sign In")
              })
          }

        }
      }
    }
    .sheet(isPresented: $showSignIn) {
      SignUpView(action: { (userName) in
        userInfo.userName = userName
      })
    }
  }
}

So then if I open this view, fill out the form and then force close the app, it doesn't have the data when I reopen the app

if I don't force close the app, then the data sticks around for a while before disappearing

Previously I was using a handful of @AppStorage(AppStorageKeys.authedUserName)


TinyStorage: 1.0.16

@christianselig
Copy link
Owner

Can you reproduce this with a sample project? I only ask because I'm not quite sure what the internals of SignUpView might be doing for instance

@christianselig
Copy link
Owner

Unable to reproduce with the following code, seems to save correctly. Please let me know if you have sample/code project and I'm happy to look into it.

import SwiftUI
import TinyStorage

struct ContentView: View {
    var body: some View {
        SettingsView()
    }
}

struct AuthUserMeta: Codable {
    var userName: String
    let createdAt: Date?
    var karma: Int?
}

struct SettingsView: View {
    @State var showSignIn = false
    
    @TinyStorageItem(AppStorageKey.authUserMeta, storage: .appGroup)
    var userInfo = AuthUserMeta(userName: "", createdAt: nil, karma: nil)
    
    var body: some View {
        VStack {
            Form {
                Section {
                    Button(
                        action: {
                            showSignIn = true
                        },
                        label: {
                            Text("Sign In")
                        })
                }
            }
        }
        .sheet(isPresented: $showSignIn) {
            VStack {
                Text("Stored Value: \(userInfo.userName)")
                TextField("Message", text: $userInfo.userName)
            }
        }
    }
}

enum AppStorageKey: String, TinyStorageKey {
  case authUserMeta
  case lastFullStorySync
}

extension TinyStorage {
    static let appGroup: TinyStorage = {
        let appGroupID = "group.com.christianselig.apollo"
        let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupID)!
        return .init(insideDirectory: containerURL, name: "tiny-storage-general-prefs")
    }()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants