我不能在CloudKit中保存记录,为什么?

时间:2016-12-27 10:56:48

标签: swift3 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]操作69CB31B15036D50D出现连接错误:   错误域= NSCocoaErrorDomain代码= 4097"与名为的服务的连接   com.apple.cloudd" UserInfo = {NSDebugDescription =与服务的连接   名为com.apple.cloudd}

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

1 个答案:

答案 0 :(得分:4)

导致此问题的非常重要的事情是使用cloudKit保存除CKRecord以外的类型的记录。在我的例子中,类Post()中的函数save()试图保存自己,这是CKRecord的子类而不是纯CKRecord。

另一件事,阻止它工作是一种描述和回答的问题here