【问题标题】:CREATE operation not permitted不允许创建操作
【发布时间】:2015-02-18 01:40:06
【问题描述】:

我尝试在我的 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)

【问题讨论】:

  • 这似乎是一个权限问题。您可以使用同一用户直接访问数据库吗?如果是这样,您可以通过直接访问创建您的记录吗?我相信第一个答案是肯定的,第二个答案是否定的。您需要授予数据库用户创建权限。
  • @LajosArpad 第一个答案是肯定的,但第二个答案也是否定的,我怎样才能给数据库用户创建权限?如果我知道如何,我觉得我会尝试这样做
  • 可能的答案是设置此处描述的创建权限:stackoverflow.com/a/56733008/1807644

标签: ios swift ios8 icloud cloudkit


【解决方案1】:

Users 是 CloudKit 中已经存在的特殊记录类型。您不应该自己创建记录。将为使用您的应用程序的新用户自动创建记录。 您也不能创建订阅和查询用户记录类型。您只能通过 ID 直接查询用户记录。您可以在用户记录类型中存储额外的数据,但我认为在您的情况下,如果您将记录类型命名为其他名称会更好。

【讨论】:

  • 谢谢,有什么办法可以得到当前用户的id,这样我就可以查询到他们的密码和用户名了?
  • 当前 iCloud 用户将只有一个 id、firstname 和 lastname。您可以添加自己的密码字段,但您的用户为什么要登录两次?它已经登录到 iCloud。您可以使用容器上的 fetchUserRecordIDWithCompletionHandler 获取您的 id。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多