Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:08:54+00:00 2026-05-25T23:08:54+00:00

Our app lets the user choose a location from a list which is provided

  • 0

Our app lets the user choose a location from a list which is provided by an API. The list updates infrequently and only by adding items, so instead of hitting the API every time, the app comes with a snapshot in a Core Data sqlite store, and we want it to periodically update the list. The code to do so works like this:

  • create a managed object context for the thread
  • get the full list from the API
  • for each one:
    • find the Location in the context with a matching locationID
    • if not found, insert a new one into the context
    • update the Location with the new information
  • save the context

When starting with a blank DB, this works fine. However, when we run it a second time it fails during the save with the message “error during SQL execution : constraint failed”. It does this even if I limit it to one location. If I turn on SQL debugging, I see the following:

CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: COMMIT
CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: INSERT INTO ZLOCATION(Z_PK, Z_ENT, Z_OPT, ZGEOID, ZCOUNTY, ZCOUNTRYCODE, ZNAME, ZLATITUDE, ZLONGITUDE, ZLANGUAGECODE) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
CoreData: error: (19) constraint failed
CoreData: annotation: Disconnecting from sqlite database due to an error.

Then it reconnects and tries again a few times before giving up.

My code is definitely finding the old Locations and the objects are all valid – or at least [object validateForUpdate] returns YES. What does the error mean? Is there a way of finding out which constraint is failing?

If I use the binary store, the error goes away – but the binary store is atomic and blocks for ages on writes. It looks like a bug in the sqlite store – has anyone found a workaround?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:08:55+00:00Added an answer on May 25, 2026 at 11:08 pm

    I noticed that it was doing INSERT INTO ZLOCATION not UPDATE ZLOCATION, so I had a look at the bit where I was inserting the locations into the context. I had:

    if ([object isInserted])
    {
        if (![object validateForUpdate:&error])
        {
            NSLog(@"Invalid object %@: %@, %@", object, error, [error userInfo]);   
            break;
        }
    }
    else
    {
        if ([object validateForInsert:&error])
        {
            [context insertObject:object];
        }
        else
        {
            NSLog(@"Invalid object %@: %@, %@", object, error, [error userInfo]);   
            break;
        }
    }
    

    What I didn’t know is that, for objects coming out of the database, [object isInserted] is false. So, I was inserting an already-inserted object and it was crashing as a result. When I changed it to [object managedObjectContext], my problem went away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our app allows multiple files to be selected in a file selection dialog which
Intermittently in our app, we encounter LockTimeoutExceptions being throw from SQL CE. We've recently
We have an admin debug console in our app that lets you enter a
Our app (already deployed) is using an Access/Jet database. The upcoming version of our
Our app uses a component that requires a license file in the directory with
Our app runs in jvm 32 bit, even when in windows x64. Now, at
Our app servers (weblogic) all use log4j to log to the same file on
In our app, we currently live with the legacy of a decision to store
Whilst trying to get our app working in Firefox (I'm a big proponent of
I want our app to show the online help page (so it's always up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.