2017-03-03 5 views
0

アプリのコアデータと今日の拡張機能と違います。しかし、私の存在は常に空です。私はURLについていくつかの問題があると思う。PersistantStoreへのカスタムパスを設定する方法

私は自分のディレクトリをチェックします。そして、私は私の文書の状態が閉じていることを確認することができます。ファイルが存在します。しかし、私は本当に開くことはできません。それをどうやって開くのですか?

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { 
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: 
     self.managedObjectModel) 
    let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group....") 

    let url = directory!.appendingPathComponent("name.sqlite") 

    let document = UIManagedDocument(fileURL: url) 

    if document.documentState == .normal { 
     NSLog("document is normal status") 
    } 

    if document.documentState == .closed { 

     let fileExist = FileManager.default.fileExists(atPath: url.path) 
     NSLog("fileExist =\(fileExist)") 

     document.open(completionHandler: { (success: Bool) in 
      if success { 
       NSLog("document is opened!") 
      }else { 
       NSLog("document is still clodsed!") 
       return 
      } 
     }) 

     NSLog("document is closed") 
    } 


    do { 
     try coordinator.addPersistentStore(ofType: 
      NSSQLiteStoreType, 
              configurationName: nil, 
              at: url, 
              options: nil) 
    } catch { 
     // Report any error we got. 
     NSLog("CoreData error \(error), \(error._userInfo)") 
     self.errorHandler(error) 
    } 
    return coordinator 
}() 

ここにログです:

ここではカスタムコアDATクラスのコアデータ方法がある

fileExist =true 
document is closed 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIManagedDocument can only read documents that are file packages' 

が正しいアクセスでは??

答えて

1

あなたはUIManagedDocument必要はありません。

let baseUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group....") 

var storeUrl = baseUrl.appendingPathComponent(momdName) 
storeUrl = storeUrl.appendingPathExtension("sqlite") 

... 

try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeUrl, options: nil) 

... 
+0

おかげでたくさん! –

0

はこの1つを試してみてください:

let fm = FileManager.default 
if let docsDir = fm.urls(for: .documentDirectory, in: .userDomainMask).first { 
      let url = docsDir.appendingPathComponent("MyDocumentName") 
      let document = UIManagedDocument(fileURL: url) 

      if document.documentState == .normal { 

      } else if document.documentState == .closed { 
       if fm.fileExists(atPath: url.path) { 
        document.open(completionHandler: { (success) in 

        }) 
       } else { 
        document.save(to: document.fileURL, for: .forCreating, completionHandler: { (success) in 

        }) 
       } 
      } 
     } 
関連する問題