Keeping a long-term reference to an IOS AddressBook entry - iphone

Given that an ABRecordID can change between cloud syncs and under other circumstances out of my control, how can I maintain a long-term reference to an IOS address book record?
Apple provides the following guidance:
"The recommended way to keep a long-term reference to a particular record is to store the first and last name, or a hash of the first and last name, in addition to the identifier. When you look up a record by ID, compare the record’s name to your stored name. If they don’t match, use the stored name to find the record, and store the new ID for the record."
But I don't understand this guidance. If the address book can have duplicate names in it AND since users can modify the name in a record how could this advice work?
For example, if the user modifies the name of an address book record my routine will fail to find it by ABRecordID so if I think search by the name hash I stored couldn't I find a duplicate name instead of the new ABRecordID for that specific record I previously referenced?
In the end, what is the BEST way to get a long-term reference to an IOS AddressBook record? And if the above advice really does work what am I missing?

The most robust (yet not completely failsafe) approach would be to come up with a priority ranking of ABRecord fields and store as much from that list as is available, along with the ABRecordID, into your own (hashed) private record format. When retrieving a private record (or at another convenient time), you can verify that the private record matches the ABRecord and work through a series of fallback checks to ensure it's accurate.
Example priority ranking:
ABRecordID
FirstName
LastName
PhoneNumber
ZipCode
When retrieving a record you can first match the ABRecordID. If that returns no results, you can do a search for FirstName + LastName. You can then match those results against PhoneNumber... etc. In this way you could potentially distinguish between 2 Bob Smiths, as they may have different phone numbers (or one may not have a phone number). Of course, depending on how long your priority list is, the more robust this mechanism will be.
The last resort would be prompting the user to distinguish between 2 Bob Smiths with brand new ABRecordID's whose records are otherwise identical -- after all, such an inconvenient prompt would be far more friendly than allowing the User to contact the wrong Bob Smith (and as I said, would be a last resort).
This solution for AB may involve some synchronization issues, however.
This is a familiar problem for anyone who has worked with the iOS Media Player. Specifically MPMediaItems in the User's Music Library have a property MPMediaItemPropertyPersistentID which the docs describe as:
The value is not guaranteed to persist across a sync/unsync/sync cycle.
In other words, the PersistentID is not guaranteed to be persistent. Solutions for this include doing similar fallback checks on MediaItem properties.

The RecordID only get changed either on delete or reset, when this is done all the new record(s) will have new createdProperty and modifiedProperty as well.
While I am reading the address book for the first time, I will save all entries of the record along with RecordID in my database.
I will save the last time the contacts synced from contacts to my database(name it something: lastSyncedTime) and store it some where.
I am done with syncing the contacts for the first time, now do the following for syncing anytime in future.
while Iterating through all records,
check createdTime(kABPersonCreationDateProperty) vs lastSyncedTime. If createdTime > lastSyncedTime, store the recordID in a "newRecords" NSArray.
If !(step 1) then check modifiedDate(kABPersonModificationDateProperty) vs lastSyncedTime. If modifiedDate > lastSyncedTime, then store the recordID in a "modifiedRecords" NSArray.
if !(1) && !(2) store all recordID in a "unModifiedRecords".
Now I will read all the contacts from my local database,
I will delete all local database records that are not find either in "modifiedRecords" or in "unModifiedRecords".
I will update all "modifiedRecords" in the local database.
I will create new records for all records in "newRecords".
Update the lastSyncedTime accordingly.

The documentation is communicating to you that you can't count on ABRecordID as a permanent identifier.
Consider this scenario: The user has a record for "Bob Smith". The user then deletes his "Bob Smith" record and then imports his contacts from his computer (creating a new ID) through iTunes sync.
So if you want to keep a permanent reference to an existing contact, you can keep a reference to the name and id as a hint that it is the same record you used before- but there is no real permanent reference.
If you keep a permanent reference to an address book contact, you must always be ready to deal with the fact that it may not be the same contact you used before.

Refer :
https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html#//apple_ref/doc/uid/TP40007744-CH6-SW2
Clearly tells you how to handle it.

Related

Issue with Vendor and Employee Business Rules

According to the documentation, there is a Business Rule for Vendor and Employee that says
The name, first name, or last name field should not be blank.
Considering that NAME is required for create, the question is ¿Does this mean that ALL three properties have to have a value on Create?
We understood that, so we ran some tests. Creating a record with the three properties populated has no problem. The issue comes when we try so insert a null value for GivenName and FamilyName (first name and last name).
Quickbooks seems to take the NULL values as valid, and when inserting the record is not returning any error. The problem is that the record that was inserted never shows in the QBD UI, but is seems like it persists somewhere, because if we try to insert it again we get a "duplicate name" error.
Not sure if this is an issue or an expected behaviour... Any hints??
Thanks
QuickBooks for windows has some legacy behavior to be aware of. The First, Last and Name combines for a unique identifier accross Customers, Vendors and Employees. So you should fill in ALL the information you have. Any attempts to insert a record with the same F or L name, will fail if there is a Customer, Vendor or Employee with the same F & L.
Second, you mentioned that the record doesn't show in QuickBooks for Windows?
Did it sync successfully? Did you check the error state of the record you inserted?
Did you look at the sync logs to see if it failed to sync?
Lastly, you need to check all three objects for a user with the same F & L name if you are getting a duplicate error. Normally you would check to see if the customer/vendor/employee exists firsts so you can update the existing or determine if it is in fact a new record.
hope that helps
Jarred

Why is ABRecordCopyCompositeName NULL, and what's an alternative?

I find that in development, in my contacts, ABRecordCopyCompositeName returns NULL for a few records, but when I view my contact list normally, I don't see any missing names.
What's a standard alternative label to use if ABRecordCopyCompositeName returns a NULL? Also, what causes the NULL values to exist in the first place?
(EDIT: title corrected)
Strangely:
The count at the bottom of the "Contacts" iPhone app states 119 contacts.
I counted all entries - all 119 are there and have names.
[ABAddressBookCopyDefaultSource(addressBook) count] returns 116, not 119.
Counting each entry from ABAddressBook, I get 116, including the 3 null entries.
So, on top of three null entries, I'm missing 3 more.
I'm still looking for a better answer, but until then, here's what I've found:
The NULL records are due to records imported from Exchange. They show as having a name, and if you edit them and save them (through the UI) the name shows up correctly, but until then, I can't seem to get a name out of the record.
The three 'missing' records are still confusing me. I believe they are also imported from Exchange, but just not included in the ABAddressBookCopyDefaultSource request.
My work-around was to not fill out table cells directly from ABAddressBook, but to copy the entries into an array first, so that the counts are correct. As I get further toward release, I may circle back to this routine and change it so that very large address books will not cause memory issues.
Not all contacts have stored firstname and lastname just email or some other field... For some reason apple is sorting based on this.. The empty compositename with email address "example#example.com" will appear below the contact named "Edward"... So it makes sense to create a fallback-thingy: if compositename is empty then look for email-address

How to check if a new contact is added to the address book?

Hey,
I'm saving a copy of all contacts in a database.
Upon launching, I want to check if there is a new contact in the address book in order to add it to my data base.
How to perform this check? I can't find the suitable logic to implement it.
There's not an easy way to get "new contacts" in the address book. Instead you'll have to do something like this:
Get an array of all the contacts in the address book.
Loop through the array and see if the contact is in your database. (NOTE: Record ids can change!)
If you find a contact that is not in your database, add it.
It's probably also smart to store the last modification date of the property and compare that with each record so you can know if something has changed on an existing record.
Finally, this could take a while depending on the number of contacts the user has. It's probably something you want to do without blocking the UI. (Another note: Address Book types are not thread safe. Keep this in mind if you're working in a background thread.)
While your app is running, you can register address book change notification callback to be alerted of changes that happen while your app is running.
A little easier way(don't need too much memory space):
Save your App's database lastest TimeStamp when you load data from AddressBook & total person count.
write a check function, and call it whenever you need it
check function : Get the latest modification date in all contacts, and get the total count right now, just compare the latest date with TimeStamp:
BOOL beNeedRefresh = NO;
if( latest > TimeStamp){
beNeedRefresh = YES;
}else if(addressbook count != your Database count){
beNeedRefresh = YES;
}
if(beNeedRefresh){
refresh your database !!!!
}

Searches (and general querying) with HBase and/or Cassandra (best practices?)

I have User model object with quite few fields (properties, if you wish) in it. Say "firstname", "lastname", "city" and "year-of-birth". Each user also gets "unique id".
I want to be able to search by them. How do I do that properly? How to do that at all?
My understanding (will work for pretty much any key-value storage -- first goes key, then value)
u:123456789 = serialized_json_object
("u" as a simple prefix for user's keys, 123456789 is "unique id").
Now, thinking that I want to be able to search by firstname and lastname, I can save in:
f:Steve = u:384734807,u:2398248764,u:23276263
f:Alex = u:12324355,u:121324334
so key is "f" - which is prefix for firstnames, and "Steve" is actual firstname.
For "u:Steve" we save as value all user id's who are "Steve's".
That makes every search very-very easy. Querying by few fields (properties) -- say by firstname (i.e. "Steve") and lastname (i.e. "l:Anything") is still easy - first get list of user ids from "f:Steve", then list from "l:Anything", find crossing user ids, an here you go.
Problems (and there are quite a few):
Saving, updating, deleting user is a pain. It has to be atomic and consistent operation. Also, if we have size of value limited to some value - then we are in (potential) trouble. And really not of an answer here. Only zipping the list of user ids? Not too cool, though.
What id we want to add new field to search by. Eventually. Say by "city". We certainly can do the same way "c:Los Angeles" = ..., "c:Chicago" = ..., but if we didn't foresee all those "search choices" from the very beginning, then we will have to be able to create some night job or something to go by all existing User records and update those "c:CITY" for them... Quite a big job!
Problems with locking. User "u:123" updates his name "Alex", and user "u:456" updates his name "Alex". They both have to update "f:Alex" with their id's. That means either we get into overwriting problem, or one update will wait for another (and imaging if there are many of them?!).
What's the best way of doing that? Keeping in mind that I want to search by many fields?
P.S. Please, the question is about HBase/Cassandra/NoSQL/Key-Value storages. Please please - no advices to use MySQL and "read about" SELECTs; and worry about scaling problems "later". There is a reason why I asked MY question exactly the way I did. :-)
Being able to query properties directly is one of the features you lose when moving away from SQL, so you need a way to maintain your own index to let you find records.
If your datastore does not have built in indexing or atomic list operations, you will need to deal with the locking issues you mention. However, indexing doesn't necessarily need to be synchronous - maintain a queue of updated records to be reindexed and you have a solution for 3 that can be reused to solve 2 also.
If the index list for a particular value becomes too large for the system to handle in a single list, you can replace the list of users with a list of lists. However, if you have that many records with the same value it probably isn't a particularly useful search criteria anyway.
Another option that is useful in some cases is to use a seperate system for the indexing - for example you could set up lucene to index the records in your main datastore.
I guess i would have implemented this as a MapReduce job, which would run on schedule.
Each search word, would be a row-key with lookup to UID.
Rowkey:uid1
profile:firstName: Joe
profile:lastName: Doe
profile:nick: DoeMaster
Rowkey: uid2
profile:firstName: Jane
profile:lastName: Doe
profile:nick: SuperBabe
MapReduse indexes all searchable properties and add them with search word as row key
Rowkey: Jane
lookup:uid: uid2
Rowkey: Doe
lookup:uid: uid2, uid1
Rowkey: DoeMaster
lookup:uid: uid1
..etc
Now, if you need to update the index list on the fly as a user change, you would write the change directly to the index base, by remove uid value from index and add to another row key. In case of this happens at the same time, temporary locking could be implemented.
For users being removed, an additional attribute telling the state of the user could be use to filter them out from search.
Adding additional search word isn't very hard, since its just about which name:value you want to index. you could filter search more also by adding type attribute to your row key/keyword. i.e boston - lookup:type: city.
The idea is to maintain your own row key based search index inside hbase.

What is the correct way to associate with a ABPerson?

In many of my apps, it requires associating some data with a contact in addressbook. What I used to do is save the record id of an ABPerson and use that id to pull information upon each app launch. However, more and more I find that this approach is wrong because many times a user will use a service like mobileme where the addressbook is wiped and resynced. This causes the record id to change and all associations are lost. The user will have to go through each one and re-link them.
What is a better approach to holding a robust pointer to addressbook entries?
You should store three values: the record ID, the first name, and the last name.
1) In the case that the record ID hasn't changed, you're golden - just use that to locate the proper record.
2) If ABAddressBookGetPersonWithRecordID() does not locate a record for your stored record ID (it returns NULL), then you'll need to search the person records for a match based on the first and last name. You can drop down to using ABAddressBookCopyPeopleWithName() potentially or write your own locating code if you already have an array with all the person records in-memory. Locating the new record is up to you. Once to locate the new record, you can update your data storage with the new record ID.
Ultimately, you end up storing the record ID to use directly incase it doesn't change (if you're lucky) plus storing some keys from the address book entry that are unlikely to change. The name of the person or organization associated with an address book entry is most likely to change. You should, of course, account for the case where you may not find a record with the stored record ID or by searching for the name. This could trivially mean that the record was deleted, or it could mean that the record was renamed. You should handle that case whichever way you decide is best for your specific application.
I know this was last year, however, I thought I might suggest a method I use. The first time I ask the user to pick a contact (in order to associate certain of my app's private data with it) I then grab the record, create my own internal record id (the initials of the app name and a sequence number usually) modify the contact by adding a new ABRelatedName (type of "pref" name of "Other") value of my own internal record id. It looks like this in the .vcf
item3.X-ABRELATEDNAMES;type=pref:BZA101
item3.X-ABLabel:_$!<Other>!$_
That way, I can simply reference that record id when i add more data about the user such as the last time the app user contacted them, etc. Seems to work for me.
Hope that helps someone.
If the address book is indeed being completely wiped and re-loaded, and the only part that doesn't change is the display name, then storing the display name as the link seems like the only option.