Home > Software engineering >  Is it possible to port the data from a UIKit/Realm app to a SwiftUI/Realm app
Is it possible to port the data from a UIKit/Realm app to a SwiftUI/Realm app

Time:12-24

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.

  1. Compiled the production UIKit app and entered some data.

  2. Closed the UIKit app and Opened the SwiftUI version.

  3. Installed RealmSwift.

  4. Created all files for the Realm models.

  5. Created a SwiftUI Button for testing to be able to retrieve all objects from Realm 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?

EDIT: FYI - I'm familiar with how Migration Blocks/Schema Versions work in Realm, what I'm not sure about is if what I'm trying to do is feasible, basically to port the Realm data from a production-ready UIKit app to a new SwiftUI app. My original thought was that as long as I had the Realm models and the migrations in my new SwiftUI app it was going to work, but now that I'm getting an error it got me wondering if what I'm doing is even possible. So the main question here is...

Is it possible to port the data from a UIKit/Realm app to a SwiftUI/Realm app?

As a bonus answar could be adding the process or what the SwiftUI app would need to contain to be able to work with the exting Realm data. Thank.

CodePudding user response:

I prefer code based answers but the question is really about a process more than the code. Also, we don't know what the models look like nor the prior migration block so let me address this at a high level

Local Realms have "Schema Versions".

Models change during development and those changes need to be tracked with a version, using a Migration allows for that to happen smoothly as data needs to be migration from one object to another, newer object.

The Migration Block is the code that updates objects from one Schema Version to another and tracks the version number - that block may actually remove older objects as well; once you gone to schema version 5, for example, it may not be possible to go back to version 1, so 'loading multiple schemes' or manually doing multiple migrations is probably not going to work.

In your source code, likely in the AppDelegate, you'll probably find the Migration Block which contains schemaVersion property. You'll need to implement a similar migration block using that same version number (or higher if your schema changed) in your SwiftUI app

See SwiftUI Migration

This could get quite complex as the original block may have multiple branches (if statements) to handle different versions so it will need to be studied closely to understand what the migrations do, and then duplicate that in the new app.

  • Related