I’m currently rewriting an app that is already in production in the AppStore. This app was written in Swift and UIKit
and now I’m re-writing it in Swift and SwiftUI
. The approach I’m following is rewriting it completely from scratch in a different file but using the same app name and Signing and Capabilities of course.
What I’m expecting is to be able to port the Realm
data from the old UIKit to the new SwiftUI app for users already using the app. The production app is already in Schema version 5
.
Here is what I have done.
- Compiled the production UIKit app and entered some data.
- Closed the UIKit app and Opened the SwiftUI version.
- Installed
RealmSwift
. - Created all files for the
Realm
models. - Created a
SwiftUI
Button
for testing to be able to retrieve all objects fromRealm
but when I tap on the button, I get the error below.
import RealmSwift
struct ContentView: View {
var body: some View {
VStack{
Button("Show Realm Objects:"){
let realm = try! Realm()// error points to this line
var allEntries = realm.objects(MyObject.self)
print("All objects: \(allEntries)")
}
}
}
}
Error
The_Weather/ContentView.swift:16: Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=1 “Provided schema version 0 is less than last set version 5.” UserInfo={NSLocalizedDescription=Provided schema version 0 is less than last set version 5., Error Code=1}
2022-12-22 12:55:56.271248-0600 The Weather[78856:1414056] The_Weather/ContentView.swift:16: Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=1 “Provided schema version 0 is less than last set version 5.” UserInfo={NSLocalizedDescription=Provided schema version 0 is less than last set version 5., Error Code=1}
(lldb)
I tried adding all five schema versions in the main app file, similar to what I have in my UIKit app but I get the same error.
import SwiftUI
import RealmSwift
struct TheWeatherApp: SwiftUI.App {
init(){
migration1()
migration2()
migration3()
migration4()
migration5()
}
var body: some Scene {
WindowGroup {
MainView()
}
}
}
Can someone please explain what the process would be to port a UIKit/Realm
app to SwiftUI/Realm
?