Swift - Unable to open a realm at path, open() failed: Operation not permitted

204 Views Asked by At

I have created a realm database, and I can fetch the data on my iOS application class on the mobile device. but I need to fetch data from the custom keyboard extension class of the same application. I got a realm. lock file operation not permitted error is thrown as

Error Domain=io.realm Code=3 "Failed to open file at path '/private/var/mobile/Containers/Shared/AppGroup/23DD1FD4-A291-4599-9D5F-84057FE6CF26/BigramFileShared.realm.lock': Operation not permitted" UserInfo={Error Code=3, NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/23DD1FD4-A291-4599-9D5F-84057FE6CF26/BigramFileShared.realm.lock, Error Name=PermissionDenied, NSLocalizedDescription=Failed to open file at path '/private/var/mobile/Containers/Shared/AppGroup/23DD1FD4-A291-4599-9D5F-84057FE6CF26/BigramFileShared.realm.lock': Operation not permitted

I tried the input source:

let strpath = userDefaultAddress?.value(forKey: "shared_bigram_file_path")
let realmURL = URL(fileURLWithPath: strpath as! String )
let config = Realm.Configuration(fileURL: realmURL)
let realm = try! Realm()

// Get our Realm file's parent directory
let folderPath = realm.configuration.fileURL!.deletingLastPathComponent().path

// Disable file protection for this directory
try! FileManager.default.setAttributes([FileAttributeKey(rawValue: FileAttributeKey.protectionKey.rawValue): FileProtectionType.none], ofItemAtPath: folderPath)
        
do {
     let realm = try Realm(configuration: config)
     let results = realm.objects(IndianSpace.self)
   } catch {
            print("Error opening Realm: \(error)")
   }
0

There are 0 best solutions below