How to migrate Realm object that hasn't been stored? - ios

I have an object that I haven't stored anywhere (yet). Even without creating a Realm for it, the app crashes saying this object requires a migration after I've modified it.
I've tried this (in method func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool):
RLMRealm.setSchemaVersion(1, forRealmAtPath: RLMRealm.defaultRealmPath()) { (migration: RLMMigration!, oldSchemaVersion: UInt) -> Void in
if oldSchemaVersion < 1 {
// not needed, nothing stored...
}
}
RLMRealm.defaultRealm()
Log:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'PYDRealmChange' due to the following errors:
- Property 'relativePath' is missing from latest object model.
- Property 'source' has been added to latest object model.
- Property 'target' has been added to latest object model.'
which is what I've found in the documentation. It didn't help and the app is still crashing. My guess is that I need the actual Realm to perform the migration on. So my question is - how do I migrate if there is none?

So problem solved:
What I didn't know is the fact, that I guess that the model object is set in all the realms existing in the app no matter whether I have stored it in them or not. The solution was to copy/paste the code RLMRealm.setSchemaVersion... for all the realm objects in my app. Then it started working.

Related

Failed to get default firebase instance in swift

As I was working on my iOS app, I got this error out of no where
Terminating app due to uncaught exception 'FIRAppNotConfigured', reason: 'Failed to get default Firebase Database instance. Must call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift) before using Firebase Database.'
on the line initializing DB_BASE
var DB_BASE = Database.database().reference()
class DataService {
static let instance = DataService()
}
I have FirebaseApp.configure() in my didFinishLaunchingWithOptions AppDelegate function, so I'm not sure what the error is all about. I've tried updating pods, restarting my computer, but no luck so far.
I haven't worked with Database specifically, but we use Storage, Analytics and Remote Config. Have you put google-services.json to the root of the project?
The code you provided doesn't make it totally clear, but if DB_BASE by any chance is a member of a singleton (like DataService.instance), try moving it to a different place. Static variables get inited before applicationDidFinishLaunching is called.

Firebase Analytics / Firebase Crashlytics - reporting errors

I am working on an iOS app that uses Firebase Analytics and Firebase Crashlytics. I wonder what is the best way to report errors. Crashes are reported automatically, so probably I should log errors as events? I am talking about caught cases where for example the data from the server cannot be parsed and used for some reason, but the app does not crash, just doesn't work as expected.
I am looking at the predefined event app_exception and its predefined parameter firebase_event_origin. Is this the right way to do it and if yes what should be logged as firebase_event_origin? Or should I define some custom event with custom params, or maybe there is a better way?
I'm doing something like this in my project and it's working great:
public protocol ErrorRecorder {
func recordError(_ error: NSError, userInfo: [String: Any]?)
}
extension Crashlytics: ErrorRecorder {
public func recordError(_ error: NSError, userInfo: [String: Any]?) {
Crashlytics.sharedInstance().recordError(error, withAdditionalUserInfo: userInfo)
}
}

Firebase configuration not working

the Firebase configuration is not working even though it is called in the app delegate method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure();
confic(application: application);
return true
}
I did not had this problem before where my classes were initialised as singletons those particular classes are dependent on Firebase however I decided to instantiate them individually in each view controller instead of using singletons however now I'm getting this issue can anyone help?.
I also tried removing any calls to Firebase from the Appdelegate class and just have FirebaseApp.configure(); included in the delegate however this did not help either.
theconfic(application: application); method contains all my Firebase related calls such as checking if the user has logged in etc.
I moved in here as in previous post I read this may help however it made no difference still getting the same error
Error message
2018-05-16 16:23:42.201080+0100 Jagdschule Gut Grambow[1983:376501] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call [FIRApp configure]; (FirebaseApp.configure() in Swift) in the App Delegate's application:didFinishLaunchingWithOptions: (application(_:didFinishLaunchingWithOptions:) in Swift).'
* First throw call stack:
(0x1841cad8c 0x1833845ec 0x1841cac6c 0x1021d8244 0x10213b510 0x1021321e8 0x1021209cc 0x102120b5c 0x18e40dedc 0x18e56f628 0x18e56f7a0 0x18e56f360 0x18e23d2c8 0x18e26b4d0 0x102097890 0x102097940 0x18e40dedc 0x18e56f628 0x18e56f360 0x18e23cf24 0x10216b674 0x10216b728 0x18e40dedc 0x18e56f628 0x18e56f360 0x18e40db84 0x18e40e31c 0x18e56f628 0x18e56f7a0 0x18e56f360 0x18e40ced4 0x18e719d88 0x18e17bfd8 0x18dd99254 0x18dd67550 0x18e397a0c 0x18dd66e4c 0x18dd66ce8 0x18dd65b78 0x18e9fb72c 0x18dd65268 0x18e7e09b8 0x18e92eae8 0x18dd64c88 0x18dd64624 0x18dd6165c 0x18dd613ac 0x1869c8470 0x1869d0d6c 0x103aa9220 0x103ab5850 0x1869fc878 0x1869fc51c 0x1869fcab8 0x184173404 0x184172c2c 0x18417079c 0x184090da8 0x186073020 0x18e07178c 0x102130644 0x183b21fc0)
2018-05-16 16:23:42.201344+0100 Jagdschule Gut Grambow[1983:376766] 5.0.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add [FIRApp configure]; (FirebaseApp.configure() in Swift) to your application initialization. Read more:
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

persistenceEnabled works in simulator, but raise a NSInternalInconsistencyException on device

I've enable Firebase's disk persistence in my app by setting persistenceEnable to true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
return true
}
When I run the app in xcode's iPhone simulator everything work as expected but when I run the app on the device I get an NSInternalInconsistencyException.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unrecognized database version: '
*** First throw call stack:
(0x18e68d1b8 0x18d0c455c 0x18e68d100 0x10014323c 0x100143124 0x100154434 0x100de9258 0x100de9218 0x100df6aec 0x100decce0 0x100df7088 0x100df8e2c 0x100df8b78 0x18d71f2a0 0x18d71ed8c)
libc++abi.dylib: terminating with uncaught exception of type NSException
I don't really know what's happening. It looks like the exception is thrown when attaching an 'observe' listener
let ref = FIRDatabase.database().reference()
ref.child("usersEvents").child(userID!).observe(.value, with: { (snapshot) in
...
If I comment the persistenceEnabled = true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
//FIRDatabase.database().persistenceEnabled = true
return true
}
The app works on my device but of course I loose the offline capabilities.
I've also tried to move the FIRDatabase.database().persistenceEnabled = true but each time I use it the app crashed when executing on the iPhone.
Thanks and regards.
TLDR;
I remove the app from my device and let xCode install a fresh copy of it and that solved the issue.
Full comment :
After some trying to reproduce the issue in an other project for several hours without success I found the solution.
The error message is not 100% clear but gives a good clue Unrecognized database version:
As the app was only crashing on my device when persistenceEnabled was set to true I figured out the issue must have been related to the local database cache.
I remove the app from my device and let xCode install a fresh copy of it and that solved the issue.
Somehow the local Firebase cache must have been corrupted and each time Firebase's sdk tried to access it my app crashed.
I hope this may help someone facing the same issue some days.

Realm Migration not working

I'm getting a crash on my App Store update, the migration is handled and even when I see the code Block is being executed I get the Following migration error
'RLMException', reason: 'Migration is required for object type
'LLCachedObject' due to the following errors:
- Property 'resultType' has been added to latest object model.'
This is how I'm handling the Migration
[migration enumerateObjects:LLCachedObject.className
block:^(RLMObject *oldObject, RLMObject *newObject) {
if (oldSchemaVersion < 5) {
newObject[#"resultType"] = kLLResultTypeBrief;
}
}];
and I double-checked that the oldSchemaVersion was 4, and newObject[#"resultType"] is being set properly, this is happening on iOS 9, I'm completely clueless as I've run out of things to checked to find out what is causing this.
So the problem turned out being that we have another project we use for the same app that also has a Realm, we weren't aware that the migration needed to be handled in both sides, so what we are doing now is using the class subsets to specify the Model Clases that every project uses.
For more details https://realm.io/docs/objc/latest/#class-subsets

Resources