Communication Error using Contact.persistChanges()

I have a script that changes the contact photos to ones from reddit, but I get “ Error: Communication Error” on a specific contact. I also have to persist the changes every 10 or so contacts or it’ll crash if I persist in a larger batch.

I assume that your script downloads the images from reddit or somewhere else and this might be the source of the communication error. One image might be on a sever that repeatedly closes the connection while downloading, but this is just a guess since I don’t know much about your script.

That you have to persist the changes every 10 contacts might be due to memory issues. I don’t know the inner workings of the app but I can imagine that there are some API restrictions from Apple and keeping more than 10 images in memory might be crossing these restrictions.

It gets a collection of posts in json and then loops through the contacts cropping the images and assigning it to the contact. It doesn’t download them. I believe the error originates from Contact.persistChanges

I don’t understand. Where do you then get the images from? Are they already assigned to the contact?
Is there a line number included with the error message? If not, wrap the code where you think the error is in a try catch block and log the error together with error.line and error.column.

The images come from reddit. They are not downloaded, but are loaded using loadImage() and then assigned to the contact. As for the communication error, it doesn’t give a line number. It appears to originate from Contact.persistChanges()

.loadImage() downloads the image to the RAM and returns its object representation (so you can assign it to a variable).

I’ve looked at the Swift documentation and there is a communication error defined, but not why it occurs. This confirms that saving the changes produces the error (I wasn’t sure because your descriptions were confusing me).

Since I don’t know how to fix the error we can only ping @simonbs and ask if he can help. Or you write him an email (you can find his email in the Scriptable app) and include your script. Without the script he will probably not be able to find the problem.

1 Like

Since it is on a “specific contact”, is there anything different about that contact’s image compared to other contacts where it is working? I’m just wondering if it might be a particularly huge original image, an animated image, or a different type/format of image (e.g. an SVG) that could be the source of the issue.

Nothing different about the contact image, name or anything else. I tried changind contact info and image manually and run it again, but didn’t help.

From what I have read so far then, it sounds like it always fails on the same specific contact, even if that is the only contact, and even if you try changing every possible value for the data for the contact, but when you apply it to a different contact, it works fine.

That makes it sound like you are completely changing the contact that fails, effectively to a different contact, but it still fails. That does not seem to make sense, so I assume I have misinterpreted something above.

Can you clarify where I have misunderstood your explanation?

You got it pretty much on the dot