adding to CNGroup always fails

Viewed 191

seems like its something super simple in the iPhone simulator the code works fine on an actual device I get a recordDoesNotExist error. basically I am just selecting a contact from the CNContactPickerViewController and adding it to a 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)
  }
 }

this works fine in the simulator but does not work on my phone, I am able to query this contact using the identifier and the contact is in the store however whenever I try to add to group I get an error "The save request failed because it updates a record that does not exist or has already been deleted."

0 Answers
Related