地址簿和核心数据 [英] AddressBook and Core Data
问题描述
我试图创建一个应用程序,将存储地址簿联系人的ID使用核心数据的SQLite文件。
I am trying to make an app that will store addressbook contacts' IDs into a SQLite file using core data.
我的问题是,我想要请求一些这些ID是随机的,并且联系人ID是不可靠的,因为在联系人ID中可能有间隙,所以我想有一种索引属性,我可以做我的随机,在索引中永远不会有间隙,如果我删除我的数据库中的一些ID,我想确保索引是重建索引(不确定如果索引是正确的名称,也许一个键)。
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.
最佳方式更新我的SQLite文件时,地址簿更改与 ABAddressBookRegisterExternalChangeCallback(addressBook,addressBookChanged,* context)
?
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部分:
编写一个获取请求,返回所有联系人实体。然后使用 -countForFetchRequest:
来确定您有多少。
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.
现在写入另一个获取请求,其中 -fetchOffset
等于随机整数, fetchLimit
of 1。
Now write another fetch request with -fetchOffset
equal to that random integer, and fetchLimit
of 1.
对于第2部分:
ABExternalChangeCallback
不列出任何将指示哪些对象改变的参数。但您可以按 kABPersonModificationDateProperty
对通讯录排序,并查看自上次同步后更改的记录。
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.
这篇关于地址簿和核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!