私は、レルム・データベースをマイグレーションし、スキーマ・バージョンを設定する方法について説明しています。私が使用しているコードの種類は次のとおりです。領域のマイグレーション、初期化先
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
これはかなり標準的なコードであり、他の人が使用するようです。しかし、私を引きつけているように見えるのは、私がRealmインスタンスを初期化して、スキーマ設定を設定したり維持したりしないところです。
何次のコードを設定する場所を、私はあるに苦しんでいます:私は、コントローラを作成する場合、それは
let uiRealm = try! Realm()
- 私が移動後に関数を呼び出すつもりで、initialiserをそれの上に置くと、それでもうまくいきません。
私は次のコードのようにViewControllerのクラス内に置くとInstancエラーEメンバーuiRealmがタイプXYZViewController
import UIKit import RealmSwift class XYZViewController: UITableViewController,UIPickerViewDataSource,UIPickerViewDelegate { let uiRealm = try! Realm() var scenarios = uiRealm.objects(Scenario).filter("isActive = true ") }
に使用することはできませんので、私の質問は:初期化するために、どのように最善の移行をどこにどのベストプラクティスがあります。
ありがとうございます。次のコードを遅延初期化に変換するにはどうすればよいですか?シナリオ= uiRealm.objects(シナリオ).filter( "isActive = true")。私がちょっと前に怠け者を置くと、「未解決の識別子uiRealmの使用」というエラーが表示される –
心配はありません!うーん、あなたはuiRealmの代わりに 'Realm()'を使うことができない理由は何ですか? – TiM
こんにちは、私はvarシナリオを宣言することでこれを取得しました:結果!各機能内の領域を初期化します。繰り返しますが、私はグローバルな食べ物としてあまりにも多くをやろうとしており、すべてを機能に移しました。 –