I'd like to create the initial data with Realm, but I don't know how to do it.

Asked 2 years ago, Updated 2 years ago, 51 views

Swift 2.1
Realm 0.98

As an image, multiple questions and answers are stored in a DB like a quiz app, and
Select from the question list to display a questionnaire and provide answers.

I just introduced Realm, and I don't really understand the overall flow.

I've seen the update documentation, and I can create a model where I can process the data.
That's all I could understand.

Could you tell me how to create an image?

ios swift realm

2022-09-30 18:08

2 Answers

When I input initial data (=seed data), I set a flag for the initial boot process and only pour it in at the first boot.
Specifically, AppDelegate is working on it, so look at the specific code (some methods are omitted due to the amount of sentences).
Please use the insertSeedData method to prepare for kishikawa katsumi's response and add a write action.

import UIKit
import RealmSwift

@UIAapplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{

    var window —UIWindow?
    let defaults = NSUserDefaults()

    func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) ->Bool{

        // initial boot process
        letdic = ["initialLaunch": true ]
        defaults.registerDefaults(dic)
        defaults.synchronize()

        if defaults.boolForKey("initialLaunch") == true {
            print("initial setup start")
            self.initialSetUp()
        }

        return true
    }

    func initialSetUp(){
        // initial migration
        self.realmMigration()
        // seed data input
        self.insertSeedData()
        // flag as first-time-launched
        self.defaults.setBool(false, forKey: "initialLaunch")
        self.defaults.synchronize()
    }

    US>funcrealmMigration(){
        let config = Realm.Configuration(
            // Configures a new schema version.Must be greater than the previous version.
            // (If you have never configured a schema version, 0 is initially configured.)
            schemaVersion: 1,

            // Describes the migration process.Attempting to open an older schema version of Realm
            // The migration runs automatically.
            migrationBlock: {migration, oldSchemaVersion in
                if(oldSchemaVersion<1){
                    // You don't have to do anything!
                    // Realm automatically recognizes newly added and deleted properties.
                    // It then automatically updates the schema on the disk.
                }
        })

        // Apply the new configuration to the default Realm.
        Realm.Configuration.defaultConfiguration=config

        // If the schema version is different when you try to open the Realm file,
        // The migration will occur automatically.
        let realm=try!Realm()
    }

    func insertSeedData(){
        // realm
        let realm=try!Realm()
        // Create an empty app user
        letappUser=User()
        appUser.id = 1
        // write processing
        try!realm.write{
            realm.add (appUser, update:true)
        }
    }
}


2022-09-30 18:08

First, consider the structure of the data questions and answers.
The minimum requirement is the text of the question and the answer.Depending on the nature of the application, the order and difficulty level may be necessary.Identify the data items you need and design the class based on them.

In Realm, the class becomes a table called in the general database.

Once you have finished creating the class, save the actual problem statement and answer data to Realm.

This is the initial data.Creating initial data is easy to create a suitable project and do with the simulator.

When initial data creation is complete, copy the resulting file into the real application project and incorporate it.
Real applications load and use embedded files.

The general flow is as above.

As for the current situation, for more information, I think it would be better to ask questions using Realm chat support (you can ask questions in Japanese) http://slack.realm.io/.


2022-09-30 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.