Moody
Moody

Reputation: 352

AddressBook and Core Data

I am trying to make an app that will store addressbook contacts' IDs into a SQLite file using core data.

My problem is i will want to request some of those IDs at random, and contact IDs is not reliable because there might be gaps in contacts ID, so i want to have a kind of index attribute that i can do my random on that can never have gaps in the indexes, and if i delete some IDs from my data base, i want to make sure that index is reindexed (not sure if index is the right name for that, maybe a key).

Does core data implement that automatically? does it create a key for any data model i create by default? if so, can i access that and take advantage of it? what happens if i delete a row?

Sorry it might be a preliminary question.

Also, whats the best way to update my SQLite file when addressbook changes with ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, *context) ? will i have to rewrite the whole addressbook again to my SQLite?

Upvotes: 0

Views: 640

Answers (1)

Hal Mueller
Hal Mueller

Reputation: 7646

For part 1:

Write a fetch request that will return all of your Contact entities. Then use -countForFetchRequest: to determine how many you have.

Generate a random integer within the range of your entity count.

Now write another fetch request with -fetchOffset equal to that random integer, and fetchLimit of 1.

For part 2: The documentation for ABExternalChangeCallback doesn't list any parameter that will denote which objects changed. But you could sort the address book by kABPersonModificationDateProperty, and look at records that have changed since the last time you synced up.

Upvotes: 1

Related Questions