1

I am working with CallKit about module Identification

Step 1: I entered the correct phone number with the country code -> identification works fine

Step 2: I add more random numbers + add country code -> Of course the numbers I random are not real -> Identification not working with the numbers random and the numbers I added the first time in step 1

My code:

private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
// Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers,
// consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
//
// Numbers must be provided in numerically ascending order.
if let identifications = try? self.fetchSpamSync() {
  for identification in identifications {
    if let name = identification.name {
      var number = String(identification.number)
      number = "084" + number
      let newNumber = Int64(number as String) ?? Int64("0")!
      if newNumber != 0 {
        context.addIdentificationEntry(withNextSequentialPhoneNumber: newNumber, label: name)
      }
    }
  }
}}

private func addOrRemoveIncrementalIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext, since date: Date) {
// Retrieve any changes to the set of phone numbers to identify (and their identification labels) from data store. For optimal performance and memory usage when there are many phone numbers,
// consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
if let identifications = try? self.fetchSpamSync(includeRemoved: true, since: date) {
  for identification in identifications {
    if identification.isRemoved {
      context.removeIdentificationEntry(withPhoneNumber: identification.number)
    } else {
      if let name = identification.name {
        var number = String(identification.number)
        number = "084" + number
        let newNumber = Int64(number as String) ?? Int64("0")!
        if newNumber != 0 {
          context.addIdentificationEntry(withNextSequentialPhoneNumber: newNumber, label: name)
        }
      }
    }
  }
}}    // Record the most-recently loaded set of identification entries in data store for the next incremental load...

Can you tell me what is causing the error with all the numbers I just added although some have worked fine and correct

Is it possible that if one of them fails, all of them won't work? although it worked fine before

Many thanks !

  • 1
    what does `self.fetchSpamSync` do? Don't use `try ?` - it throws away any errors. Use `do/try/catch` and at least print any errors. is the `identifications` array sorted by `number` ascending? Ideally your numbers will already be in e.164 format, rather than manipulating them in your extension, but it is pointless adding `084` - Just add `84`. Leading 0s are discarded. – Paulw11 Sep 16 '22 at 08:32
  • Also you have a bug with removal - You are manually adding the prefix (084), but you don't add this prefix when you call `removeIdentification`. – Paulw11 Sep 16 '22 at 08:34
  • Thank @Paulw11, ***self.fetchSpamSync*** function perform data retrieval from core data. I'm trying remove prefix ***084*** and prefix ***084*** exist in ***identification.number*** make sure country code correct and sorted by number ascending. This error occurs when i add about 100 random numbers – a programer Sep 16 '22 at 08:45
  • I'm trying add twos number of my friends this works fine, but error occurs when i try to add ***numbers that i random*** with country code prefix – a programer Sep 16 '22 at 08:47
  • 1
    I would not recommend using core data in a callkit extension. Extensions have a very small memory limit. It is better to share a simple text file (even json) from your main app – Paulw11 Sep 16 '22 at 09:41
  • I have not thought of a solution if I use json file, how will it sync with the server? You can you give me some information about this? Many thanks – a programer Sep 16 '22 at 09:56
  • 1
    Much the same as core data. When you get your updates from your server in your main app, you just write a json file to your app group folder. Your main app can continue to use core data if you like – Paulw11 Sep 16 '22 at 10:03
  • if I continue to use Core Data, when the data is up to 30000, 100000 or even millions, will Core Data be able to meet? Thank you – a programer Sep 16 '22 at 11:42
  • 1
    From my experience, using core data you will exceed the memory capacity of the extension at about 5000 numbers. I have upwards of 60,000 numbers using a text file. – Paulw11 Sep 16 '22 at 13:25
  • Wow, great! I think i need change from Core Data to text file. But i don't understand ***Write a json file to your app group folder***, You can give me documents or tutorial for this issue? I'm code React Native, newbie IOS. CallKit is a module Native for my project React Native, Thanks YOU – a programer Sep 17 '22 at 04:00
  • 1
    You must already be using an app group to share your code data database between your main app and your extension. It is similar. You need to get the app group folder and then write a file there. https://www.npmjs.com/package/react-native-file-access – Paulw11 Sep 17 '22 at 11:07
  • Hi @Paulw11, I've been researching the lib you sent, I see they provide 2 functions that I can use are ***FilesSystem.getAppGroupDir(groupName: string): Promise***, ***FileSystem.writeFile(path: string, data: string, encoding?: 'utf8' | 'base64'): Promise***. But in order to use the ***getAppGroupDir*** function, maybe I have to create Group Dir under native IOS first, right? then i call ***getAppGroupDir*** function get path and finally call ***FileSystem.writeFile***, right? – a programer Sep 22 '22 at 09:34
  • Yes, you will need to add the app group entitlement in the underlying Xcode project – Paulw11 Sep 22 '22 at 10:22
  • I've handled to write the file to the app group folder, can I ask more is the data returned by the backend is the type of text to write to the file or the file? – a programer Sep 23 '22 at 02:08
  • I don't understand the question – Paulw11 Sep 23 '22 at 05:55
  • I'm sorry, I'm just ask logic to performance sync between server and app. – a programer Sep 23 '22 at 06:41
  • Thanks a lot @Paulw11, I'll take care of this myself – a programer Sep 23 '22 at 06:51
  • oh. I would keep the core data in the main app where you don't have memory constraints. Your back end can provide delta updates or whatever is efficient and then you can easily read the core data records to create whatever text file you need – Paulw11 Sep 23 '22 at 07:08
  • Hi @Paulw11, can I ask more? and sorry if I annoy you. For example, I added this phone `number: 09884558` and `name: MTP` to the extension, thereafter i add again with this phone number but `name` is `Son Tung M-TP` will cause an error? if not, which name will take precedence? – a programer Sep 23 '22 at 09:20
  • The most recent name for the number will take precedence. – Paulw11 Sep 23 '22 at 09:58
  • Wow, I'm understanding that when I add a number into identification, old the number that already exists will update with name. Is it right? – a programer Sep 23 '22 at 10:03
  • Or is it still added but will show the latest name? – a programer Sep 23 '22 at 10:04
  • 1
    What's the difference? When you set a name for the number, the number is either added with that name or the name for the number is updated. You don't need to remove it first and then re-add it – Paulw11 Sep 23 '22 at 10:33
  • @Paulw11 Hi Paul, long time no see. Please help me, sometimes my extension doesn't work, i'm thinking it's because extension reloads too many times, i noticed there is requestFailed(for:withError:) function, maybe when there is any error with extension this function will be call? and can i restart the extension in that function? many thanks – a programer Jun 30 '23 at 07:17
  • @Paulw11, please help me, I just created 1 more extension, but the second extension I just created is not enabled if user upgrade from old version, otherwise if new user it works Good – a programer Jul 01 '23 at 04:17

0 Answers0