AddressBook and Core Data

半腔热情 提交于 2019-12-12 02:03:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/24733195/addressbook-and-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!