Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果应用程序在iOS上未处于活动状态,我如何知道联系人已被修改?_Ios_Swift_Cncontact_Cncontactstore - Fatal编程技术网

如果应用程序在iOS上未处于活动状态,我如何知道联系人已被修改?

如果应用程序在iOS上未处于活动状态,我如何知道联系人已被修改?,ios,swift,cncontact,cncontactstore,Ios,Swift,Cncontact,Cncontactstore,当联系人发生变化时,我需要运行一个函数。如果应用程序处于活动状态,您可以使用本节所述的NotificationCenter执行此操作(有时,当我向现有联系人添加新号码时,此功能会起作用)。我如何知道应用程序启动后联系人(或多个联系人)已更改?我为我的任务设置了以下功能 @objc private func matchingContacts() { if isSuccessContactUploading { contactManager.matchin

当联系人发生变化时,我需要运行一个函数。如果应用程序处于活动状态,您可以使用本节所述的
NotificationCenter
执行此操作(有时,当我向现有联系人添加新号码时,此功能会起作用)。我如何知道应用程序启动后联系人(或多个联系人)已更改?

我为我的任务设置了以下功能

  @objc private func matchingContacts() {
        if isSuccessContactUploading {
            contactManager.matchingContacts(notMatch: { [weak self] in
                guard let _self = self else { return }
                debugPrint("matchingContacts != equals")
                _self.isSuccessContactUploading = false
                _self.syncContacts()
            })
        }
    }
这些功能位于ContactManager中

   func matchingContacts(notMatch: (() -> Void)?) {
        getContacts { (contacts, error) in
            if error == nil {
                debugPrint("contacts count", contacts.count)
                self.getContactsDictionaryFromCache(contacts, notMatch: {
                    notMatch?()
                })
            }
        }
    }

 private func getContactsDictionaryFromCache(_ contacts: [CNContact], notMatch: (() -> Void)?) {
        var isMatching = true
        for contact in contacts {
            let key = contact.identifier

            do {
                let cache = try Cache<NSDictionary>(name: "Contacts")
                if let contactDictionary = cache[key] {
                    if !contactDictionary.isEqual(to: contact.dictionary) {
                        debugPrint("contactDictionary not matching")
                        isMatching = false
                    }
                } else {
                    debugPrint("contactDictionary isn't here")
                    isMatching = false
                }
            } catch {
                debugPrint(error.localizedDescription)
                isMatching = false
            }
        }

        if !isMatching {
            notMatch?()
        }

        cacheContacts(contacts)
    }

private func cacheContacts(_ contacts: [CNContact]) {
        for contact in contacts {
            let contactDictionary = contact.dictionary as NSDictionary
            let key = contact.identifier

            do {
                let cache = try Cache<NSDictionary>(name: "Contacts")
                cache[key] = contactDictionary
            } catch {
                debugPrint(error.localizedDescription)
            }
        }
    }
func匹配联系人(不匹配:(()->Void)?){
getContacts{(联系人,错误)位于
如果错误==nil{
debugPrint(“联系人计数”,contacts.count)
self.getContactsDictionaryFromCache(联系人,不匹配:{
不匹配?()
})
}
}
}
私有函数getContactsDictionaryFromCache(contacts:[CNContact],notMatch:(()->Void)?){
var isMatching=true
用于联系人中的联系人{
让key=contact.identifier
做{
let cache=尝试缓存(名称:“联系人”)
如果让contactDictionary=缓存[键]{
if!contact.dictionary.isEqual(收件人:contact.dictionary){
debugPrint(“contactDictionary不匹配”)
isMatching=false
}
}否则{
debugPrint(“contactDictionary不在这里”)
isMatching=false
}
}抓住{
debugPrint(错误。本地化描述)
isMatching=false
}
}
如果!我匹配{
不匹配?()
}
缓存联系人(联系人)
}
private func cacheContacts(contacts:[CNContact]){
用于联系人中的联系人{
让contactDictionary=contact.dictionary作为NSDictionary
让key=contact.identifier
做{
let cache=尝试缓存(名称:“联系人”)
cache[键]=contactDictionary
}抓住{
debugPrint(错误。本地化描述)
}
}
}

我认为操作系统不会通知您联系人商店的任何更改。尝试对联系人标识符(或您需要检查已更改的属性)和应用程序启动时进行散列,并在需要时再次对其进行散列。@Sealos我会这样做的,我只是觉得突然之间有了一个更好的解决方案。