realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Improve exception message if old Realm opened in read-only mode & migration needed

Open Ustimov opened this issue 8 years ago • 6 comments

Goals

After successful migration at first run, app crashes at next startup with strange error. I want app startup without crash any time.

Expected Results

Correctly completed app startup.

Actual Results

2017-02-03 12:40:41.458 gym[3600:77841] *** Terminating app due to uncaught exception 'RLMException', reason: 'Provided schema version 2 is less than last set version 1.'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010e4d6d4b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010df3821e objc_exception_throw + 48
	2   gym                                 0x000000010d141474 _Z18RLMSetErrorOrThrowP7NSErrorPU15__autoreleasingS0_ + 980
	3   gym                                 0x000000010d0f8e1c _Z26RLMRealmTranslateExceptionPU15__autoreleasingP7NSError + 652
	4   gym                                 0x000000010d0fadb0 +[RLMRealm realmWithConfiguration:error:] + 5648
	5   gym                                 0x000000010ceb325a -[UAGAppDelegate application:didFinishLaunchingWithOptions:] + 1034
	6   UIKit                               0x000000010e9e63c2 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 290
	7   UIKit                               0x000000010e9e7d47 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4236
	8   UIKit                               0x000000010e9ee0ed -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
	9   UIKit                               0x000000010e9eb26d -[UIApplication workspaceDidEndTransaction:] + 188
	10  FrontBoardServices                  0x0000000111a0a6cb __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
	11  FrontBoardServices                  0x0000000111a0a544 -[FBSSerialQueue _performNext] + 189
	12  FrontBoardServices                  0x0000000111a0a8cd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
	13  CoreFoundation                      0x000000010e47b761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	14  CoreFoundation                      0x000000010e46098c __CFRunLoopDoSources0 + 556
	15  CoreFoundation                      0x000000010e45fe76 __CFRunLoopRun + 918
	16  CoreFoundation                      0x000000010e45f884 CFRunLoopRunSpecific + 420
	17  UIKit                               0x000000010e9e9aea -[UIApplication _run] + 434
	18  UIKit                               0x000000010e9efc68 UIApplicationMain + 159
	19  gym                                 0x000000010ceb426f main + 111
	20  libdyld.dylib                       0x00000001103c468d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Steps to Reproduce

  1. Clone and open demo project
git clone https://github.com/Ustimov/RealmMigrationIssue.git
cd RealmMigrationIssue/
pod install
open gym.xcworkspace
  1. Run project in simulator (I use iPhone SE simulator)

  2. After app starts stop it

  3. Run app yet another time

  4. Get crash with

Terminating app due to uncaught exception 'RLMException', reason:
'Provided schema version 2 is less than last set version 1.'

Code Sample

https://github.com/Ustimov/RealmMigrationIssue

Version of Realm and Tooling

Realm version: 2.4.2

Xcode version: 8.2.1

iOS/OSX version: iOS 10.2 (Simulator)/ OS X El Capitan 10.11.6

Dependency manager + version: CocoaPods 1.0.1

Ustimov avatar Feb 03 '17 09:02 Ustimov

Hi @Ustimov. Thanks for reaching out. One of our engineers will look into the issue and follow-up with a solution or some additional questions. Cheers.

pigeon-archive avatar Feb 03 '17 17:02 pigeon-archive

Thanks!

Now I figured out what crashing depends on sequence of realms opening.

All I want is merge current user realm and new bundled realm which comes with update.

So, if I first open current user realm (oldRealm) then first app run is ok and ALL next runs results in app crash.

RLMRealm * oldRealm = [RLMRealm realmWithConfiguration:oldDbConfig error:nil];
NSLog(@"Old realm url: %@", [oldRealm configuration].fileURL);
        
RLMRealm * realm = [RLMRealm defaultRealm];
NSLog(@"Default realm url: %@", [realm configuration].fileURL);

If I change the order then first run is ok, ONLY second run results in crash and all next runs is ok.

I'm not sure if it's bug or I'm doing something wrong, but anyway exception message is confusing.

UPDATE: I'm sorry. I found what migration is failing because of opening oldRealm in read only mode. So, it's enough provide more clear exception description for this case.

Ustimov avatar Feb 05 '17 08:02 Ustimov

Hi @Ustimov. Thanks for providing this information. ~~Would you be able to provide us with a sample project that reproduces what you're describing?~~

My apologies. Saw the link to the project above. Let me review it.

pigeon-archive avatar Feb 10 '17 17:02 pigeon-archive

I'm sorry. I found what migration is failing because of opening oldRealm in read only mode. So, it's enough provide more clear exception description for this case.

Or are you saying you've resolved the issue?

pigeon-archive avatar Feb 10 '17 17:02 pigeon-archive

I resolve the issue, but in my opinion the exception message does not provide clear description of problem source.

Ustimov avatar Feb 10 '17 19:02 Ustimov

Thanks. We'll turn this into a ticket to track improving the exception message.

austinzheng avatar Feb 10 '17 19:02 austinzheng