[ad_1]
I’ve a view which exhibits duplicate contacts and permits the person choose from them and create a brand new occasion and likewise add it to the CNStore
var duplicateContacts: [String:[CNContact]]
var physique: some View {
VStack {
textOnTop
removeDuplicatesToggle
ScrollView(.vertical,showsIndicators: false) {
ForEach(keys,id: .self) { key in
VStack {
let mergedContact = getMergedContact(for: key)
textOverMergedContact
mergedContactView(mergedContact: mergedContact,for: key)
divider(for: key)
duplicateContactsView(for: key)
mergeButton(contact: mergedContact,for: key)
}
.body(width: contactsViewWidth)
.background(Shade.white)
.cornerRadius(rectCornerRadius)
.transition(.slide)
}
}
}
.body(width: UIScreen.important.bounds.width,top: UIScreen.important.bounds.top * 0.9,alignment: .high)
.background(Shade.colorThemes.popUpViewBackgroundColor)
.cornerRadius(rectCornerRadius)
}
@ViewBuilder
personal func mergeButton(contact: CNMutableContact,for quantity: String)-> some View {
Button {
withAnimation(.easeIn(period: 0.5)) {
homeVM.addContactToTheStore(contact: contact)
if deleteDuplicatesAfterMerging, let contactsToRemove = selectedContacts[number] {
homeVM.deleteContactsFromTheStore(contactsToRemove)
}
selectedContacts[number] = []
homeVM.reloadContatsData()
}
} label: {
ButtonWithImageAndTitle(imageName: "Merge", title: "Объединить (selectedContacts[number]?.depend ?? 0) контакта".localized(), backgroundColor: backgroundColorForMergeButton(key: quantity),width: contactsItemsWidth)
.animation(.linear(period: 0.2), worth: selectedContacts)
.opacity((selectedContacts[number]?.isEmpty ?? true) ? 0.5 : 1)
}
}
The megredContactView exhibits that the mergedContact accommodates a quantity
Complete view
After tapping to the mergeButton the contact passes to the viewModel
public func addContactToTheStore(contact: CNMutableContact) {
contactManager.addContact(contact)
}
Which directs it to the contact supervisor
func addContact(_ contactToAdd: CNMutableContact) {
let saveRequest = CNSaveRequest()
saveRequest.add(contactToAdd, toContainerWithIdentifier: nil)
print("Contact numbers: (contactToAdd.phoneNumbers)")
do {
attempt retailer.execute(saveRequest)
print("Including contact :(contactToAdd.givenName) with numbers: (contactToAdd.phoneNumbers)")
} catch {
print("Error occured whereas making an attempt to save lots of contact instanse to retailer: (error.localizedDescription)")
}
}
That is the place the issue is. Though, it prints out the console:
Including contact :DuplicateName with numbers: [<CNLabeledValue: 0x600000b3dd40: identifier=30269647-53EE-47E2-A9EA-C909D46D66BA, label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x600001070d80: stringValue=+998 99-944-55-25, initialCountryCode=(nil)>, iOSLegacyIdentifier=0>, <CNLabeledValue: 0x600000b38bc0: identifier=FC498300-429F-4F3E-8155-F38EE78E7EF8, label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x60000107f6f0: stringValue=+998 99-507-55-25, initialCountryCode=(nil)>, iOSLegacyIdentifier=1>]
The contact is added with none properties besides the identify, new contact has neither numbers nor emails
[ad_2]
