I'm trying to implement a contacts reader in Xamarin.iOS which tries to iterate over the iOS contacts in all CNContactStore containers. Instead of loading all contacts into memory, I need to iterate over a contacts resultset batch by batch (paging contacts). However all the examples that I saw in SO load almost all contacts into memory first.
i.e. This question has loads of similar examples that read all contacts at once. Although these examples have logic which iterates one by one, it is not evident to me how to skip N and take the next N number of contacts without iterating from the beginning on the next call (which looks sub optimal at least to me).
Apple's own documentation reads
When fetching all contacts and caching the results, first fetch all contacts identifiers, then fetch batches of detailed contacts by identifiers as required
I was able to do this easily for Android using the cursor based approach available in its SDK. Is this at all possible for iOS? If not how can we handle a large number of contacts (e.g. something above 2000, etc.). I don't mind examples in swift. I should be able to convert them to Xamarin.
Thanks in advance.
Here's the approach I took, granted my requirements did not allow persisting contacts, only holding in active memory. Not saying it's the right approach, but fetching all identifiers first, then lazily fetching all keys for a specific contact as needed, did improve performance. It also avoids performing a lookup when the contact doesn't exist. I also tried using NSCache instead of dictionary, but ran into issue when I needed to iterate over the cache.
I truncated functions that aren't relevant to the topic but hopefully still convey the approach.