I have a code that needs to be ran about every year to update some files at the app launch and I'm trying to come up with the best way to do that. What I need to be able to basically force the app to run the code whenever I need to update the files.
This is what I came up with that I think would work but I'm not sure if that's the best way.
Are there any other options to handle this type of logic?
App version 1 - INITIAL RELASE.
This code would work as long as I don't add an update.
// First launch
@main
struct SwifUIPlayGroundApp: App {
@AppStorage("shouldLoad") var shouldLoad = true
init(){
if shouldLoad{
print("Loading...")
shouldLoad = false
}else{
print("No need to relaod...")
}
}
}
App version 2 - UPDATE 1.
Here I would need to add a second variable to force the update shouldUpdate
. I would also need to change the logic to check for the shouldUpdate
instead of the shouldLoad
and set the shouldLoad
to true
to be prepared for future updates.
// UPDATE 1
@main
struct SwifUIPlayGroundApp: App {
@AppStorage("shouldUpdate") var shouldUpdate = true // new
@AppStorage("shouldLoad") var shouldLoad = true
init(){
if shouldUpdate{
print("Loading...")
shouldUpdate = false
shouldLoad = true // prepare for next update
}else{
print("No need to relaod...")
}
}
}
App version 3 - UPDATE 2.
Here I would need to change the logic back to check for the shouldLoad
instead of the shouldUpdate
and set the shouldUpdate
to true
to be prepared for future updates.
// UPDATE 2
@main
struct SwifUIPlayGroundApp: App {
@AppStorage("shouldUpdate") var shouldUpdate = true
@AppStorage("shouldLoad") var shouldLoad = true
init(){
if shouldLoad{
print("Loading...")
shouldUpdate = true // prepare for next update
shouldLoad = false
}else{
print("No need to relaod...")
}
}
}
App version 4 - UPDATE 3.
Repeat what I did in UPDATE 1...
So everything occurs in the app. Right ?
Which means user has first to upgrade to new release for anything to happen. Right ?
If so, why not having a flag in the JSON, which tracks that file Has Changed Since Previous Version ?
And reload CoreData if true.
However, if user skips an update, flag may be false… even though user should update.
So, may be the best is to not have a Bool flag but a version number flag, indicating when file last changed. Comparing to last version on the device (kept in AppStorage), you know if you have to load or not.
And when the app is first installed, nothing in AppStorage yet (or just a version 0 by default, so that comparison triggers the load); so you know you have to load Coredata.