I am trying add new column in my existing structure by following guide line : https://realm.io/docs/swift/latest/#migrations but app gets crashed as and when i am trying to get default realm object using RLMRealm.defaultRealm()
Migration code:
func applicationDidFinishLaunching(aNotification: NSNotification) {
let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()
configuration.schemaVersion = 1
configuration.migrationBlock = {(migration:RLMMigration, oldSchemaVersion: UInt64) in
if oldSchemaVersion < 1 {
//Migration code
}
}
RLMRealmConfiguration.setDefaultConfiguration(configuration)
let realm = RLMRealm.defaultRealm() //Getting exception on this line
}
Exception logs:
2015-10-20 00:44:03.162 RealmTest[60656:33271965] *** Terminating app due to uncaught exception 'RLMException', reason: 'Provided schema version 0 is less than last set version 1.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8e48703c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8684a76e objc_exception_throw + 43
2 Realm 0x000000010017e738 _Z18RLMSetErrorOrThrowP7NSErrorPU15__autoreleasingS0_ + 264
3 Realm 0x0000000100158bbe +[RLMRealm realmWithConfiguration:error:] + 5598
4 Realm 0x0000000100156d1d +[RLMRealm defaultRealm] + 93
5 RealmTest 0x00000001000028bb _TFC9RealmTest7Trainer11saveTrainerfS0_FT_T_ + 75
6 RealmTest 0x0000000100004224 _TFC9RealmTest14ViewController13viewDidAppearfS0_FT_T_ + 612
7 RealmTest 0x0000000100004352 _TToFC9RealmTest14ViewController13viewDidAppearfS0_FT_T_ + 34
Required details:
ProductName: Mac OS X
ProductVersion: 10.10.5
BuildVersion: 14F27
/Applications/Xcode7.app/Contents/Developer
Xcode 7.0
Build version 7A220
/usr/bin/pod
0.39.0
Realm (0.96.1)
/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
(not in use here)
/usr/bin/git
git version 2.3.8 (Apple Git-58)
@bipinvaylu It seems you have access to the Realm in the view controller before the configuration, migration block and schema version are set. Because error message indicates that the schema version is set 0 (It's default value, that means not set).
Regarding stack traces, the crash occurred in the viewDidAppear method. Please make sure that the configuration object is set before viewDidAppear method is called.
thanks @kishikawakatsumi, will look at this later. Currently stuck at another realm issue.
Hey @bipinvaylu, after we could resolve #2720, any update on this?
Hey @mrackwitz, It's seems working fine now. Issue was realm object was created before migration code execution. Great thanks for help.
@bipinvaylu How do u solve this problem? I stuck for a long time.
@Hstripe I was facing this issue because of accessing realm object RLMRealm.getDefaultRealm() before executing migration code.
let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()
configuration.schemaVersion = 1 //Increment version when your realm class gets updated
configuration.migrationBlock = {(migration:RLMMigration, oldSchemaVersion: UInt64) in
// Migration code (Update Realm table or just delete all tables)
// I am Deleting table, Realm create new with updated fields
migration.deleteData(forClassName: Genre.className()) //Genre is Realm class
}
Most helpful comment
@bipinvaylu It seems you have access to the Realm in the view controller before the configuration, migration block and schema version are set. Because error message indicates that the schema version is set 0 (It's default value, that means not set).
Regarding stack traces, the crash occurred in the
viewDidAppearmethod. Please make sure that the configuration object is set before viewDidAppear method is called.