[
https://issues.apache.org/jira/browse/CB-10976?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15231307#comment-15231307
]
Sarangan Rajamanickam commented on CB-10976:
--------------------------------------------
I have completed my analysis and summarized my results below:
*Why is the provided code not working?*
In both iOS and Android, if you retrieve a contact, modify something in it and
save it, cordova (contacts plugin) accepts it and updates the value. But, it
does not delete the existing values. What does this mean?
*Scenario 1*
For Contact A - Work: 123-456-7890 & Home: 234-678-7654, If you add a Phone
number Mobile: 345-876-9876, the scenario will work correctly. Contact A will
be updated with a new mobile number 345-876-9876. So, Contact A will be like:
Contact A - Work: 123-456-7890 & Home: 234-678-7654 & Mobile: 345-876-9876
*Scenario 2*
For Contact A - Work: 123-456-7890/Home: 234-678-7654, If you add a Phone
number Home: 345-876-9876, the scenario will work. But not as you expected. The
existing home contact will NOT be updated. But a new Home phone number will be
added. So, Contact A will be like:
Contact A - Work: 123-456-7890 & Home: 234-678-7654 & Home: 345-876-9876
*Scenario 3*
For Contact A - Work: 123-456-7890/Home: 234-678-7654, If you add a Phone
number Home: 234-678-7654 (Note: The Home number is same as existing home
number), no updates will happen. So, Contact A will be like:
Contact A - Work: 123-456-7890 & Home: 234-678-7654
With the understanding of these 3 scenarios, we could see why the specific code
change (in this issue) is not working.
# The User creates a Contact with 3 phone numbers (work/mobile/home)
# On the saved contact, the user deletes 2 phone numbers and just leaves the
home number.
# Now, the user saves the modified contact and expects the 2 other phone
numbers will be deleted.
But, as seen on Scenario 2 & 3, this will not work. The cordova (contacts
plugin) takes in the provided one phone number and tries to update it. Since it
already has the same value, there is no real change. So, at the end, the
contact has 3 phone numbers as in beginning.
*How do we remove the existing phone numbers?*
You could clone the object - delete the original contact - modify the cloned
object and save it. So, the steps for your application will be:
# Create a contact with 3 Phone Numbers (work/mobile/phone) and save it.
# Retrieve the saved contact and clone it to a new object (using the clone api
in the contacts plugin)
# Delete the contact
# Modify the cloned object
# Save the cloned object
I have tested the same in iOS and Android and it is working fine. I have
included the code snippet here for your reference:
# You could find the HTML code here: [http://pastebin.com/rgecrmUH]
# You could find the Javascript code here: [http://pastebin.com/g5Z4Sn88]
If you modify your code based on the code provided (in my pastebin link) you
could remove the phone number fields from a given contact.
*Additional Note*
I see that you are using the 'delete' operator to remove the elements in your
JS array. While this will work with iOS, this will crash the application (while
trying to save) as Android implementation (of contacts plugin) uses JSONArray
object and using the delete operator leaves it in an unstable state. So, I
recommend using a new array instead of using 'delete' operator on the existing
array. (so it works fine on both iOS and Android)
*Next Steps*
# Issue mentioned by the reporter has been resolved and a correct method to
remove a phone number from a contact has been provided (with code snippet
reference)
# The README.md file of cordova-plugin-contacts must be updated to reflect the
steps involved in updating the phone number. A new JIRA ticket [CB-11041] has
been created and linked to this ticket.
# As I see no work items remaining on this ticket, I am resolving this ticket.
If you still find any issues or need more clarifcation, feel free to reopen
this ticket.
> Can't remove phonenumber field from contact
> -------------------------------------------
>
> Key: CB-10976
> URL: https://issues.apache.org/jira/browse/CB-10976
> Project: Apache Cordova
> Issue Type: Bug
> Components: Plugin Contacts
> Affects Versions: 2.0.0
> Environment: iOS 9.3
> Reporter: Tobias Mücksch
> Assignee: Sarangan Rajamanickam
> Labels: iOS, triaged
>
> Short demo script:
> {code:javascript}
> navigator.contacts.find(["*"], function (contacts) {
> // Pick a contact
> // (Make sure the contact has more than two phoneNumberFields)
> var myContact = contacts[4];
> // Remove the second phone number
> delete myContact.phoneNumbers[1];
> myContact.save(function () {
> console.log("Success");
> /// You will see, that the contact still has all it's previous
> phone numbers
> }, function () {
> console.error("error while saving");
> });
>
> }, function () {
> console.error("Could not access conacts.");
> });
> {code}
> Let's say a existing contact that I've retrieved from the system has two or
> more phone number fields. When I remove one of the both contact fields from
> the phoneNumbers array and then save the contact, *it still has all numbers
> on the device*. When I re-retrieve the contact it has both numbers again.
> (Tested on iOS 9.3 with plugin version 2.0.1)
> *I can't find any hint in the documentation if I do something wrong or if the
> plugin is behaving wrong.*
> I have created a demo script, which reproduces the issue. You can use it for
> testing:
> http://pastebin.com/XRdREL3Y
> You might want to remove line 25 which removes the contact in the end.
> This SO Question might be related to the issue:
> http://stackoverflow.com/questions/35698798/cordova-contacts-plugin-contact-data-is-duplicating-instead-of-overwriting-conta?rq=1
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]