我无法将记录保存在CloudKit中,为什么?

问题描述:

我试图运行一个应用程序,除了其他的东西将学校类加载到表视图。代码如下所示:我无法将记录保存在CloudKit中,为什么?

import UIKit 
import CloudKit 

class AddPostVC: UIViewController { 

    @IBOutlet weak var postText: UITextView! 
    let database = DatabaseController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard)) 

     view.addGestureRecognizer(tap) 
    } 

    //Calls this function when the tap is recognized. 
    func dismissKeyboard() { 
     //Causes the view (or one of its embedded text fields) to resign the first responder status. 
     view.endEditing(true) 
    } 

    @IBAction func postButtonPressed() { 
     let newPost = Post() 

     newPost.text = postText.text 
     newPost.user = CKReference(recordID: CKRecordID(recordName: UserDefaults.standard.string(forKey: "userRecordID")!), action: CKReferenceAction.none) 

     newPost.save() { _, error in 
      if error == nil { 
       DispatchQueue.main.async { 
        self.navigationController?.popViewController(animated: true) 

        self.dismiss(animated: true, completion: nil) 
       } 
      } else { 
       if !self.database.networkCheck() { 
        DispatchQueue.main.async { 
         self.displayErrorMessage(message: "It seems that there is no internet connection, try again") 
        } 
       } else { 
        DispatchQueue.main.async { 
         self.displayErrorMessage(message: error.debugDescription + ". Contact Apple or us for help") 
        } 
       } 
      } 

     } 

    } 

    func displayErrorMessage(message:String) { 

     let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert) 

     let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil) 

     alert.addAction(dismissAction) 

     self.present(alert, animated: true) 

    } 

} 

当代码运行,将会返回我一个错误:

[LogFacilityCK] Got a connection error for operation 69CB31B15036D50D: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.cloudd" UserInfo={NSDebugDescription=connection to service named com.apple.cloudd}

有谁知道如何解决这个问题?到目前为止,我还没有在互联网上找到任何解决方案。

导致此问题的非常重要的事情是将与CKKecord以外的其他类型的记录保存在一起。在我的例子中,函数save()在类Post()中试图保存自己,这是CKRecord的一个子类,而不是纯CKRecord。

另一件事,停止它的工作是描述和回答here

+0

在回答您自己的问题并将其作为答案发布方面非常出色。 – MwcsMac

+0

真的很有帮助。谢谢Danny。 – EmptyStack