Closed maciekish closed 8 years ago
Where and how do you initialize Night.currentNight
? That would need to happen from the main thread. Is that the case?
It is called on the main thread, thats also where it crashes. I changed the code slighlty to always reuse the same Realm() object after configuring.
This is the implementation for currentNight
:
class var currentNight: Night? {
get {
let allNights = ConfiguredRealm.realm.objects(Night)
// Find night in progress
if let lastNight = allNights.last {
if !lastNight.completed {
return lastNight
}
}
return nil
}
}
And this is ConfiguredRealm.realm
:
class ConfiguredRealm {
static var realm: Realm = {
let config = Realm.Configuration(
schemaVersion: 3,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// Nothing to do!
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
return try! Realm()
}()
}
Is the above code the problem? Should i always get a new Realm()
or is reuse like this fine?
The reason for this solution is that the configuration must be created and set (?) before Realm is accessed the first time, but the app could be launched in multiple ways (appDidFinishLaunching, state restoration etc) so this is a way to guarantee that.
Thanks for your time!
It is called on the main thread, thats also where it crashes. I changed the code slighlty to always reuse the same Realm() object after configuring.
That's fine. Did that fixed the issue, you were facing?
Is the above code the problem? Should i always get a new
Realm()
or is reuse like this fine?
That's not necessary. But with the above code, you would neither be able to close the Realm nor get a correctly configured Realm for another thread than the main thread.
You could setup the defaultConfiguration
instead in your AppDelegate's initialize
method. That should be enough. The first access to the Realm can then happen lazily.
And suddenly as if by magic it started working. Git confirms that the only changes are still the two .Framework
folders.
All i did was replace them again (after reverting earlier), enable zombie objects and address sanitizer and run the project in the profiler. After that it just worked, even after disabling zombie objects and address santizier.
I did clean, delete derived data and remove the app from the simulator before reporting the issue.
Any idea what happened here? It's a bit scary to be honest.
Okay, well that's odd behavior. Perhaps something went wrong on the download? If the frameworks didn't even changed since then, I really can't say.
That's fine. Did that fixed the issue, you were facing?
No, not until i did the above.
That's not necessary. But with the above code, you would neither be able to close the Realm nor get a correctly configured Realm for another thread than the main thread. You could setup the defaultConfiguration instead in your AppDelegate's initialize method. That should be enough. The first access to the Realm can then happen lazily.
Do you mean like this? (In the AppDelegate
)
override init() {
super.init()
let config = Realm.Configuration(
schemaVersion: 3,
migrationBlock: { migration, oldSchemaVersion in
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
}
What am i missing by not being able to close the Realm?
Yeah, the initializer is probably better than the +initialize
method.
What am i missing by not being able to close the Realm?
You won't be able to reset the database contents by deleting it or seed your Realm with a bundled file.
Goals
Upgrade Realm from 0.98.1 to 0.98.3
Expected Results
App should work as before since there should be no breaking changes between the two. Nothing else has changed in the project except for the two
.Frameworks
. I have tried it twice.Actual Results
App crashes with
EXC_BAD_ACCESS
Steps to Reproduce
Replace Realm.Framework and RealmSwift.Framework and run your app.
Code Sample
This is the code at line 94, line 95 is the crashing one (second line in this snippet)
Night.currentNight
just returns the last object from a RealmList<Night>
andNight.currentNight.completedSteps
is just aList<StringWrapper>
At the time of the crash there was a
Night.currentNight
object present and theNight.currentNight.completedsteps
List
was empty but notnil
.It's worth noting that I'm new to Realm and I may be doing something fundamentally wrong.
Version of Realm and Tooling
0.98.1 before and 0.98.3. Xcode 7.2.1.