Mitemmetim
Mitemmetim

Reputation: 742

How to fix 'sqlite3' and other errors in CallKit Directory extension?

The idea of the app is to add around 50.000 contacts to the CallKit Directory, but when I try to add them with context.addIdentificationEntry(withNextSequentialPhoneNumber: contact.number, label: contact.contactLabel) in the beginRequest(with context: CXCallDirectoryExtensionContext) method i often either get a com.apple.CallKit.error.calldirectorymanager error 2 or some strange sqlite3 error:

sqlite3_step for query 'INSERT INTO PhoneNumberIdentificationEntry 
(extension_id, phone_number_id, label_id) VALUES (?, (SELECT id 
FROM PhoneNumber WHERE (number = ?)), (SELECT id FROM Label WHERE 
(localized_label = ?))), 
…
(SELECT id FROM Label WHERE (localized_label = ?)))' returned 19 
(2067) errorMessage 'UNIQUE constraint failed: 
PhoneNumberIdentificationEntry.extension_id, 
PhoneNumberIdentificationEntry.phone_number_id, 
PhoneNumberIdentificationEntry.label_id'

Generally I fetch the contacts from a server and try to keep them in sync - therefore, I save them locally with Realm. Has anyone ideas how to handle such errors?

Upvotes: 2

Views: 892

Answers (2)

Marco
Marco

Reputation: 1686

It seems like you're adding duplicate contacts. Just make sure the contacts you're adding are unique.

Upvotes: 1

Mitemmetim
Mitemmetim

Reputation: 742

Since I could not find much solutions to CallKit Directory errors, I tried to fix the errors by myself. It took me some time and to save some time for other developers I try to sum up all errors I got during the implementation of the CallKit Directory Extension with a suggested solution for each of them - I cannot guarantee that these solutions work for 100% but maybe they still can help!

  • sqlite3_step error: I often got the error when I tried to sync the contacts again, therefore I guess, that the error is some kind of duplicate error. Since it is possible that a contact has two numbers, I used the same name for both entries which basically worked, but I think there is still some kind of identification problem in the background if you use the exact same name for the label. Therefore, I added the index of each contact at the end of the label to keep them unique and now the error does not appear again anymore. (Even if all of your contacts have only one number keep in mind, that there are maybe some contacts with the same first and last name.)

  • com.apple.CallKit.error.calldirectorymanager error 2: This error appears often due to memory problems. Extension have much less memory available than the app itself (CallKit Directory Extension has for example only 12 MB available). To prevent this error I used a paging mechanism which always only loads a specific amount of contacts into the extension and then gets reloaded with the next page. If you use Realm there are additionally two aspects to consider: First, set the used object types in the Realm configuration Realm(configuration: Realm.Configuration(fileURL: realmFileURL, objectTypes: [ContactObject.self])) - this prevents Realm from calling objc_copyClassList() which needs a lot of memory. Second, if you filter the realm objects which a specific predicate try to avoid using any kind of references in the filter - for example, first, after adding the contact to the CallKit Directory, I passed the number of the contact to the filter of the realm objects to mark that contact as synced. But then I saw that the memory in the extension increased with each synced contact since there were probably some kind of references to the contact object. Then, I fetched the exact same page of contacts to mark them all as synced instead of fetching them one by one which allowed me to load much more contacts per page. Tip: If you want to use Breakpoints in the extension or want to debug the memory of the extension go in Xcode to Debug -> Attach to Process -> AppName Call Directory. Then you can select the call directory in the debug navigator and see the used amount of memory. (You can only attach to the extension process when it is currently used - just in case you can’t see it in the list)

  • com.apple.CallKit.error.calldirectorymanager error 3: The contact numbers are not ordered - order them before adding them to the directory.
  • com.apple.CallKit.error.calldirectorymanager error 4: Duplicated Entries - ensure to add a number only once.

Please do not hesitate to correct me if I am wrong with any of these suggestions or to add further tips and solutions - otherwise I am glad if I could help!

Upvotes: 6

Related Questions