Core Data: Error Domain=NSCocoaErrorDomain Code=257

I am trying to access data stored in Core Data in a previous release of an app for a bundle ID with a new app which I'm trying to get to access this data and send to an external API over the internet.

The persistent container in the old app is set up like this:

   private lazy var persistentContainer: NSPersistentContainer = {
    let url = Bundle(for: ModelManager.self).url(forResource: "GIRC", withExtension: "momd")
    let model = NSManagedObjectModel(contentsOf: url!)
    let retval = NSPersistentContainer(name: "GIRC", managedObjectModel: model!)
    let desc = NSPersistentStoreDescription()
     
    desc.url = NSPersistentContainer.defaultDirectoryURL().appendingPathComponent("GIRC.sqlite")
    desc.type = NSSQLiteStoreType
    desc.shouldAddStoreAsynchronously = false
     
    retval.persistentStoreDescriptions = [desc]
    retval.loadPersistentStores { (desc, error) in
      if error == nil {
        var values = URLResourceValues()
         
        values.isExcludedFromBackup = true
         
        try? desc.url?.setResourceValues(values)
      }
    }
     
    return retval
  }()

so in the new app, I use this code to access this persistent container and files stored in there:

   public func loadDefaultData(lastVersion version: Int, completion: @escaping (Int) -> Void) {
    self.performInBackgroundContext { (context) in
      let decoder = JSONDecoder()

      let defaultFormURL = self.bundle.url(forResource: "GIRC", withExtension: "momd")!
       
      let isSecuredURL = defaultFormURL.startAccessingSecurityScopedResource() == true
// PROBLEM HAPPENS HERE:       
      let defaultFormData = try! Data(contentsOf: defaultFormURL)
      let forms = try! decoder.decode(Forms.self, from: defaultFormData)
      var currentVersion = version
       
JSONSerialization.data(withJSONObject: forms)

             
            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
              print(JSONString)
            }
       
      guard let defaultForm = forms.forms.first else {
        DispatchQueue.main.async {
          completion(version)
        }
        return
      }
       
      if defaultForm.version > currentVersion {
        Form.serializeFromStruct(defaultForm, context: context)
        currentVersion = defaultForm.version
      }
       
      self.saveContext(context)
      DispatchQueue.main.async {
        completion(currentVersion)
      }
       
      if (isSecuredURL) {
        defaultFormURL.stopAccessingSecurityScopedResource()
      }
    }
  }

At the problem happens here line of code, under "Data" this error message appears:

GIRC_Medical_Expert_App/ModelManager.swift:92: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “GIRC.momd” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/containers/Bundle/Application/FE0F0038-5BFE-47BA-B489-53DD0F4BF12E/GIRC Medical Expert App.app/GIRC.momd, NSUnderlyingError=0x2808729a0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
2021-06-08 18:07:00.164099-0500 GIRC Medical Expert App[650:144630] GIRC_Medical_Expert_App/ModelManager.swift:92: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “GIRC.momd” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/containers/Bundle/Application/FE0F0038-5BFE-47BA-B489-53DD0F4BF12E/GIRC Medical Expert App.app/GIRC.momd, NSUnderlyingError=0x2808729a0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}

I have tried adding entitlements to a plist file and also the following "SecurityScopedResource()" solution, neither have worked.

      let isSecuredURL = defaultFormURL.startAccessingSecurityScopedResource() == true

The original app was signed by a different developer in a different developer account and the app was in his account. The original app was transferred to the account where it currently lives where I am a member and am signing it that way. I am not sure if it is even possible to access the Core Data stored by the old app with a new app released with the same bundle ID. If possible though, I would like to know how.

Thank you in advance for any help 😃

I am aware the code at and below

let forms = try! decoder.decode(Forms.self, from: defaultFormData)

will not work because the data is not in json binary format, but I hope to easily remedy that by changing any json code. I want to access the Data in the Core Data persistent container before I change the code that parses it.

The solution to my core data problem has nothing to do with the code i posted. I can't delete though.

Core Data: Error Domain=NSCocoaErrorDomain Code=257
 
 
Q