Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ios) Fixing crash in iOS #651

Merged
merged 1 commit into from
Nov 14, 2024
Merged

(ios) Fixing crash in iOS #651

merged 1 commit into from
Nov 14, 2024

Conversation

robbiehanson
Copy link
Contributor

Steps to reproduce:

if you delete a contact then the ios app crashes (and will keep crashing). The issue is in CloudKitContactsDb.fetchQueueBatch which assumes that a contact exist (line 124) and if does not the db query fails and the app crashes

The problem function:

fun getContact(contactId: UUID): ContactInfo? {
  return database.transactionWithResult {
    queries.getContact(contactId = contactId.toString()).executeAsOneOrNull()?.let {
      val offers = it.offers.split(",").map {
        OfferTypes.Offer.decode(it)
      }.filterIsInstance<Try.Success<OfferTypes.Offer>>().map {
        it.get()
      }
      ContactInfo(contactId, it.name, it.photo_uri, it.use_offer_key, offers)
    }
  }
}

Even though the function signature suggests there's no problem if the contact doesn't exist, it still crashes when that's the case. My understanding is that it's due to the complex query being performed under the hood:

getContact:
SELECT
  id,
  name,
  photo_uri,
  use_offer_key,
  contacts.created_at,
  updated_at,
  group_concat(offer, ',') AS offers
FROM contacts AS contacts
JOIN contact_offers AS contact_offers ON contact_id = id
WHERE id = :contactId
ORDER BY contact_offers.created_at;

In other words, a simple query like the one below wouldn't suffer the same crash, and would just return an empty result set:

getContact2:
SELECT *
FROM contacts
WHERE id = :contactId;

So I've fixed the crash by adding an extra check to see if the contact exists before fetching it.

Note also that this crash is fixed via PR #640, because the query has been changed to the simple version above.

@robbiehanson robbiehanson requested a review from dpad85 November 13, 2024 18:47
@dpad85 dpad85 merged commit 8003dd5 into master Nov 14, 2024
@dpad85 dpad85 deleted the ios-crash-fix branch November 14, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants