Failed to open file at path: Operation not permitted

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintableJSON

      How frequently does the bug occur?

      Sometimes

      Description

      In Firebase we use NonFatal errors and sometimes our users can't use the realm, that has Failed to open file at path: Operation not permitted error. We can't catch it locally. Some info, that we can say about it.

      ErrorCode: 3
      DeviceState: 50% background
      iOS: 15, 16, 17
      

      In Firebase this is written for all errors:

      RAM free: 0 B
      Disk free: 0 B 
      

      But when this error occurs, all users have free spaces (2GB, 50GB, 20GB...).

      This is the code to get Realm.

      func getRealm(for type: DBType, canRetry: Bool = true) -> Realm? {
          guard
              self.canUseRealm == true,
              let url = type.fileUrl
          else { return nil }
      
          let config = Realm.Configuration(
              fileURL: url,
              schemaVersion: self.schemaVersion,
              migrationBlock: type.migrationBlock,
              objectTypes: type.modelTypes
          )
      
          do {
              return try Realm(configuration: config)
          } catch let error as NSError {
              if canRetry, let folderPath = config.fileURL?.deletingLastPathComponent().path {
                  try? FileManager.default.setAttributes([
                      FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication
                  ], ofItemAtPath: folderPath)
                  return getRealm(for: type, canRetry: false)
              }
              handleRealmError(type: type, config: config, error: error)
              return nil
          }
      }
      

      This is my DBType

      enum DBType: String, CaseIterable {
          case youtubeVideos
          case playerQueue
      
          private var folderName: String {
              return rawValue.prefix(1).uppercased() + rawValue.dropFirst()
          }
      
          var fileUrl: URL? {
              guard let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
              let fileManager = FileManager.default
              let path = url.appendingPathComponent("RealmDB/\(self.folderName)")
              fileManager.checkExistingForFolderPath(path)
              let fileName = String(rawValue.map { $0.isUppercase ? "_\($0.lowercased())" : String($0) }.joined())
              return path.appendingPathComponent("\(fileName).realm")
          }
      
          var modelTypes: [Object.Type] {
              switch self {
              case .youtubeVideos:
                  return [TMYoutubeVideo.self]
              case .playerQueue:
                  return [QueueDatabaseModel.self]
              }
          }
      
          var migrationBlock: MigrationBlock? {
              var block: MigrationBlock?
              switch self {
              case .youtubeVideos:
                  block = { migration, oldSchemaVersion in
                      if oldSchemaVersion < 11 {
                          migration.enumerateObjects(ofType: TMYoutubeVideoState.className()) { _, newObject in
                              newObject?["id"] = UUID().uuidString
                          }
                      }
                  }
              default:
                  break
              }
              return block
          }
      }
      

      In my code getRealm function is written as a lazy var, Here I write it as a getter to clearly understand. And I call this method from utility queue.

      Who can help?

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      No

      Reproduction Steps

      No response

      Version

      10.44.0

      What Atlas Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      iOS

      Build environment

      Xcode version: ...
      Dependency manager and version: ...

              Assignee:
              Unassigned
              Reporter:
              Unito Sync Bot
              Archiver:
              Marc Greenfield

                Created:
                Updated:
                Archived: