영역 데이터베이스를 마이그레이션하고 스키마 버전을 설정하는 문서화 된 방법 중 하나를 사용하여 작업하려고합니다. 내가 사용하고있는 코드의 종류는 다음과 같습니다.영역 마이그레이션, 초기화 위치
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
이것은 꽤 표준적인 코드로 보이며 다른 사람들이 사용하는 것 같습니다. 그러나, 나를 위로 트리핑 것으로 보인다는 설정 또는 지속되지 않는 스키마 설정을 일으키는 영역 인스턴스 initialising 오전입니다.
무엇을 다음과 같은 코드를 설정 어디가 고민입니다 : 내가 컨트롤러를 만드는 경우는
let uiRealm = try! Realm()
- 파일, 그 이동 내에서 함수를 호출하고자하는, 그리고 그것의 상단에 이니셜 라이저를 넣어 아직 작동하지 않습니다
내가 아래 코드에서와 같이 ViewController 클래스 내에 넣어 Instanc 오류 초기화하는 위치에 어떤 모범 사례가 있으며, 최선의 방법 마이그레이션 : 전자 멤버 uiRealm는
import UIKit import RealmSwift class XYZViewController: UITableViewController,UIPickerViewDataSource,UIPickerViewDelegate { let uiRealm = try! Realm() var scenarios = uiRealm.objects(Scenario).filter("isActive = true ") }
그래서 내 질문은 형 XYZViewController 사용할 수 없습니다.
감사합니다. 다음을 게으른 초기화로 변환하려면 어떻게해야합니까? 시나리오 = uiRealm.objects (시나리오) .filter ("isActive = true"). 방금 전에 게으른 것을 넣으면 오류 "미확인 식별자 uiRealm 사용" –
걱정 마세요! 음, uiRealm 대신에'Realm() '을 사용할 수없는 이유가 있습니까? – TiM
안녕하세요, 저는 var 시나리오를 선언하여 다음을 얻었습니다. 결과! 그리고 각 기능 내에서 영역을 초기화합니다. 다시 한 번 전 세계적으로 먹을 수있는 음식으로 너무 많은 일을 시도하고 모든 것을 기능으로 옮겼습니다. –