添加到CNGroup总是失败

时间:2017-08-05 14:05:04

标签: ios swift cncontactstore

在iPhone模拟器中看起来像是超级简单的东西,代码在实际设备上正常工作我得到recordDoesNotExist错误。基本上我只是从CNContactPickerViewController中选择一个联系人并将其添加到CNGroup

func getGroup(cb:@escaping (CNGroup?)->()){
    do{
        let groups = try store.groups(matching: nil).filter{$0.name == "someGroup"}

        if let group = groups.first{
            print("got a group")
            return cb(group)
        }
        let g = CNMutableGroup()
        g.name = "someGroup"
        let sr = CNSaveRequest()
        sr.add(g, toContainerWithIdentifier: nil)
        try store.execute(sr)
        return getGroup(cb:cb) //try again this might create a indefinate loop if try doesnt stop execution until end of do statement
    }catch{
        DispatchQueue.main.async {
            self.present(Alerter("Error", "encountered an error fetching the group"), animated: true)
            return cb(nil)
        }
    }
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
 getGroup{group in
  guard group != nil else {return}
  let sr = CNSaveRequest()
  sr.add(contact, to:group!)
  do{
    try self.store.execute(sr)
  }catch{
    print(error)
  }
 }

这在模拟器中工作正常,但在我的手机上不起作用,我能够使用标识符查询此联系人,并且联系人在商店中,但是每当我尝试添加到组时,我都会收到错误"保存请求失败,因为它更新了一条不存在或已被删除的记录。"

0 个答案:

没有答案