Realm-cocoa: Realm not working in Notification Service Extension

Created on 13 Dec 2016  路  2Comments  路  Source: realm/realm-cocoa

I am trying to access my Realm Database from the service extension. I tried to save it in the access group, but the extension crashes. After that i tried to just create a new Realm database in the extension with let realm = try! Realm(), but even that is not working. The extension quits with Program ended with exit code: 0. I also tried with error handling do { let realm = try Realm() } catch let error as Error{ print(error) }, but it is never executing the print statement, the extension just quits.
I checked if the location is read-only, but that is not the case.
I am using Realm 2.1.1, Xcode 8.2 and iOS 10.2

T-Help

Most helpful comment

It is probably due to running out memory. Realm() invoke objc_copyClassList() internally, it requires large amount of memory. It often fails on app extensions often because available memory is limited (very few) on extension.

To avoid this, you can specify schemas explicitly using Realm.Configuration. When giving schemas explicitly, Realm doesn't invoke objc_copyClassList(), because no need to enumerate all classes.

let config = Realm.Configuration(objectTypes: [SomeClass.self, AnotherClass.self])
let realm = try Realm(configuration: config)
...

All 2 comments

It is probably due to running out memory. Realm() invoke objc_copyClassList() internally, it requires large amount of memory. It often fails on app extensions often because available memory is limited (very few) on extension.

To avoid this, you can specify schemas explicitly using Realm.Configuration. When giving schemas explicitly, Realm doesn't invoke objc_copyClassList(), because no need to enumerate all classes.

let config = Realm.Configuration(objectTypes: [SomeClass.self, AnotherClass.self])
let realm = try Realm(configuration: config)
...

Thanks! This fixed it!

Was this page helpful?
0 / 5 - 0 ratings