Description
Goals
Open a realm that is not the default realm by using RLMPathForFile
Expected Results
The realm opens successfully with write permissions
Actual Results
The thread crashes with:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "open() failed: Operation not permitted" UserInfo={NSLocalizedDescription=open() failed: Operation not permitted, Error Code=1}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50
Steps to Reproduce
I am having trouble reproducing the issue. I haven't actually seen it happen (and testers haven't reported crashing) which leads me to believe this is happening in the background when iOS launches the app to perform a background fetch. Could write permissions be different in a background state? I remember when iOS 7 launched there were a lot of issues with the Keychain and background fetches, I wonder if something similar is happening here.
Code Sample
Basic initialization of the Realm. SDKNameSpace
is really just a bundle id like "com.company.App"
:
public static func setup(schemaVersion schemaVersion: UInt64? = nil, inMemoryIdentifier: String? = nil) {
Realm.Configuration.defaultConfiguration = Realm.Configuration(
path: RLMRealmPathForFile("\(SDKNameSpace).realm"),
schemaVersion: schemaVersion ?? RealmSchemaVersion,
migrationBlock: migrationBlock,
inMemoryIdentifier: inMemoryIdentifier
)
}
Additional Notes
This issue seems interesting: #3264
I noticed that on startup Realm was slow to load so in application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
I call the setup function above and then immediately after, run an empty write to the realm which seems to "pre-boot" it in a sense.
Edit: One thing I just thought of, I call that setup function in both application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
and application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
and I wonder if that is causing a timing issue. I added a guard to make sure the SDK is only initialized once, I'll report back any additional findings.
Version of Realm and Tooling
RealmSwift 0.93.0
XCode 7.2.1
iOS 9.2.1, iOS 9.3.0