I finally came back to this question.
Again, I'm trying to duplicate the Objective-C code on this post on stackoverflow to Swift.
Here's the Objective-C code from the post:
CNChangeHistoryFetchRequest *fetchHistory = [[CNChangeHistoryFetchRequest alloc] init];
fetchHistory.startingToken = [[NSUserDefaults standardUserDefaults] dataForKey:@"CNContactChangeHistoryToken"];
NSError *error = nil;
CNContactStore *store = [[CNContactStore alloc] init];
CNFetchResult *fetchResult = [store enumeratorForChangeHistoryFetchRequest:fetchHistory error:&error];
NSEnumerator *enumerator = [fetchResult value];
id object;
while ((object = [enumerator nextObject])) {
// do something with object
NSLog(@"change history enumerator object = %@", object);
CNChangeHistoryEvent *historyEvent = (CNChangeHistoryEvent *) object;
if ([historyEvent isKindOfClass:[CNChangeHistoryDropEverythingEvent class]]) {
NSLog(@"change history - DROP EVERYTHING!");
[historyEvent acceptEventVisitor: self];
} else {
if ([historyEvent isKindOfClass:[CNChangeHistoryAddContactEvent class]]) {
CNChangeHistoryAddContactEvent *addContactEvent = (CNChangeHistoryAddContactEvent *) object;
NSLog(@"change history - AddContact event container %@ - %@", addContactEvent.containerIdentifier, addContactEvent.contact);
} else if ([historyEvent isKindOfClass:[CNChangeHistoryUpdateContactEvent class]]) {
CNChangeHistoryUpdateContactEvent *updateContactEvent = (CNChangeHistoryUpdateContactEvent *) object;
NSLog(@"change history - UpdateContact event - %@", updateContactEvent.contact);
} else if ([historyEvent isKindOfClass:[CNChangeHistoryDeleteContactEvent class]]) {
CNChangeHistoryDeleteContactEvent *deleteContactEvent = (CNChangeHistoryDeleteContactEvent *) object;
NSLog(@"change history - DeleteContact event - %@", deleteContactEvent.contactIdentifier);
}
}
}
Here's my Swift code, as far as I got:
public func tryChangeHistoryFetchRequest() {
let fetchHistory = CNChangeHistoryFetchRequest()
let store = CNContactStore()
print("*****")
let fetchResult: CNFetchResult<NSEnumerator<CNChangeHistoryEvent>> = fetchHistory
}
// Enumerate through each visit function and check if I get anything back from fetch request.
class ChangeHistoryEventVisitor: NSEnumerator, CNChangeHistoryEventVisitor {
override func nextObject() -> Any? {
let changeHistoryDropEverythingEvent = CNChangeHistoryDropEverythingEvent()
return {}
}
func visit(_ event: CNChangeHistoryDropEverythingEvent) {
print("CNChangeHistoryDropEverythingEvent")
}
func visit(_ event: CNChangeHistoryAddContactEvent) {
}
func visit(_ event: CNChangeHistoryUpdateContactEvent) {
}
func visit(_ event: CNChangeHistoryDeleteContactEvent) {
}
}
public func tryChangeHistoryEventVisitor() {
print("*****")
let changeHistoryEventVisitor = ChangeHistoryEventVisitor()
print("*****")
let changeHistoryDropEverythingEvent = CNChangeHistoryDropEverythingEvent()
changeHistoryEventVisitor.visit(changeHistoryDropEverythingEvent)
}
class ChangeHistoryAddContactEvent: CNChangeHistoryAddContactEvent {
}
class CNChangeHistoryFetchResult: CNFetchResult<NSEnumerator<CNChangeHistoryEvent>> {
}