Skip to content

Commit 3a038db

Browse files
author
aforge
committed
Synchronize contacts after app becomes active.
1 parent ba708b4 commit 3a038db

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Tinodios/Cache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Cache {
7171
authenticateWith: Cache.kApiKey,
7272
persistDataIn: dbh.sqlStore)
7373
tinode!.OsVersion = UIDevice.current.systemVersion
74+
// Tell contacts synchronizer to attempt to synchronize contacts.
75+
ContactsSynchronizer.default.appBecameActive()
7476
}
7577
}
7678
}

Tinodios/account/ContactsSynchronizer.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,26 @@ class ContactsSynchronizer {
6868
// Watch contact book changes.
6969
NotificationCenter.default.addObserver(
7070
self, selector: #selector(contactStoreDidChange), name: .CNContactStoreDidChange, object: nil)
71+
NotificationCenter.default.addObserver(
72+
self, selector: #selector(self.appBecameActive),
73+
name: UIApplication.didBecomeActiveNotification,
74+
object: nil)
7175
}
7276

7377
@objc func contactStoreDidChange(notification: NSNotification) {
7478
Cache.log.info("Contact change: notification %@", notification)
7579
self.run()
7680
}
7781

82+
@objc
83+
func appBecameActive() {
84+
if self.authStatus == .authorized {
85+
self.run()
86+
} else {
87+
Cache.log.debug("Can't perform contact sync: unauthorized")
88+
}
89+
}
90+
7891
private func fetchContacts() -> [ContactHolder2]? {
7992
let keysToFetch: [CNKeyDescriptor] = [
8093
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),

0 commit comments

Comments
 (0)