我也有这个问题。经过一些调试后,我意识到我导致了额外的EKEventStoreChangedNotification
调用(例如,通过创建或删除EKReminder
s)。当EKEventStoreChangedNotification
我执行estore.commit()
.
我通过像这样声明全局变量来解决这个问题:
// the estore.commit in another function causes a new EKEventStoreChangedNotification. In such cases I will set the ignoreEKEventStoreChangedNotification to true so that I DON'T trigger AGAIN some of the other actions.
var ignoreEKEventStoreChangedNotification = false
然后在AppDelegate.swift
我听的地方这样做EKEventStoreChangedNotification
:
nc.addObserver(forName: NSNotification.Name(rawValue: "EKEventStoreChangedNotification"), object: estore, queue: updateQueue) {
notification in
if ignoreEKEventStoreChangedNotification {
ignoreEKEventStoreChangedNotification = false
return
}
// Rest of your code here.
}
在我对 estore 进行更改的函数中,我这样做:
//
// lots of changes to estore here...
//
ignoreEKEventStoreChangedNotification = true // the estore.commit causes a new EKEventStoreChangedNotification because I made changes. Ignore this EKEventStoreChangedNotification - I know that changes happened, I caused them!
try estore.commit()
全局变量并不漂亮(特别是如果您从事函数式编程),但它确实有效。