不允许CREATE操作

时间:2014-12-19 06:11:33

标签: ios swift ios8 icloud cloudkit

我正在尝试在我的iOS应用程序中使用CloudKit,但是当我尝试为User记录类型(记录类型命名为Users)创建新记录时,我得到这个错误:

<CKError 0x7fb80947ffb0: "Permission Failure" (10/2007);
server message = "CREATE operation not permitted";
uuid = 8C4C7B60-E3F4-42FC-9551-D3A76A6FF9D6;
container ID = "iCloud.com.jojodmo.Blix">

要创建新记录,我在viewDidLoad()中使用此代码:

UserCKManager.create(
    CloudKitUser(
        email: "email@website.com", password: "password", salt: "1AF4E759B20FEC32",
        username: "jojodmo", posts: [], biography: "This is my bio",
        firstName: "John", lastName: "Doe", flags: [],
        profilePicture: UIImage(), downVotesGiven: 1, downVotesRecieved: 2,
        upVotesGiven: 3, upVotesRecieved: 4, pictureProvided: false), callback: nil)

这会创建一个新的CloudKitUser

init(email: String, password: String, salt: String, username: String, posts: [String], biography: String, firstName: String, lastName: String, flags: [String], profilePicture: UIImage, downVotesGiven: Int, downVotesRecieved: Int, upVotesGiven: Int, upVotesRecieved: Int, pictureProvided: Bool){ 
    self.ready = false
    record = CKRecord(recordType: "Users")
    self.email = email
    self.password = password
    self.salt = salt
    self.username = username
    self.posts = posts
    self.biography = biography
    self.firstName = firstName
    self.lastName = lastName
    self.flags = flags
    self.profilePicture = profilePicture
    self.downVotesGiven = downVotesGiven
    self.downVotesRecieved = downVotesRecieved
    self.upVotesGiven = upVotesGiven
    self.upVotesRecieved = upVotesRecieved
    self.pictureProvided = pictureProvided
    self.ready = true
}

然后尝试用它创建一条记录:

class func create(user: User, callback: ((success: Bool, user: User?) -> ())?){
    let ckUser = CloudKitUser(user: user)
    //I've also tried using CKContainer.defaultContainer().privateCloudDatabase, and I get the same error
    CKContainer.defaultContainer().publicCloudDatabase.saveRecord(ckUser.record){(record, error) in
        if(error != nil){
            println("An error occurred: \(error)")
            callback?(success: false, user: nil)
        }
        else{
            println("Record saved successfully")
            callback?(success: true, user: ckUser)
        }
    }
}

我知道连接到数据库不是问题,因为我已经收到错误,说没有容器有我给出的ID,我修复了。

我试图在iOS模拟器上运行它(我已登录iCloud)

2 个答案:

答案 0 :(得分:1)

用户是CloudKit中已存在的特殊记录类型。你不应该自己创建记录。系统会自动为使用您应用的新用户创建记录。 您也无法创建订阅并查询Users记录类型。您只能通过ID直接查询用户记录。您可以在Users recordType中存储额外的数据,但我认为如果您将记录类型命名为其他内容会更好。

答案 1 :(得分:0)

除了Edwin的回答之外,将自定义字段保存到公共 Users recordType是完全可行的,但您需要先为用户检索CKRecordID(通过CKContainer fetchUserRecordID()discoverUserIdentity()),然后使用该ID在CKRecord上构建Users

此外,如果您使用CKModifyRecordsOperation,请确保将savePolicy设置为.changedKeys,以便允许更新通过。