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

  • Home
  • SEARCH
  • 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 3322828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:13:46+00:00 2026-05-17T23:13:46+00:00

I’ve got the basics of inserting records and deleting records with Core Data; however,

  • 0

I’ve got the basics of inserting records and deleting records with Core Data; however, I’d appreciate help with one of the most common functions – the insert/update.

Basically, I use NSMutableArray arrayWithContentsOfURL to get an array that contains rows from a mysql table. What I need to do is now sync up my CoreData store.

In other words, I need to add every row in the array to my CoreData table but if it already exists I need to update the record with the latest values. Also if it exists in Core Data and not in the downloaded array, I’d need to delete it.

I probably could hack this together; however, I’d like to see how its properly and efficiently done without memory leaks.

  • 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-17T23:13:46+00:00Added an answer on May 17, 2026 at 11:13 pm

    There are two ways to insert data into Core Data – and whichever one you use is up to you. However, one of them depends on whether you have generated Model classes for your data model for the Core Data db.

    The regular way is to use the following:

    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
    inManagedObjectContext:context];
    [object setValue:@"value1" forKey:@"stringColumn"];
    [object setValue:12 forKey:@"numberValue"];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Failed to save - error: %@", [error localizedDescription]);
    }
    

    This is assuming you’ve already got your managed object context set up.
    It is much more efficient if you create and insert your objects into the context in a loop, and then save after the loop ends.

    The other method isn’t much different, but is much safer in terms of type safety. If you have generated model classes (which you can do from the
    xcdatamodels) then you can simply create an object of that class and set its properties.

    TableObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
    inManagedObjectContext:context];
    [object setStringColumn:@"value1"];
    [object setNumberValue:12];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Failed to save - error: %@", [error localizedDescription]);
    }
    

    To delete from a table, simply retrieve the object from the table (I’m assuming you are using the second method here for insertions, and as such have generated model classes) and use the following:

    [context deleteObject:object];
    

    Note that you will need to call save for that to take effect as well.

    Hope this helps! Good luck!

    EDIT:
    Sorry, I must’ve misread the question!

    To examine an existing record, you’ll want to create a Fetch Request, and then execute it on your managed object context.
    At bare minimum, a Fetch Request requires an entity (so it knows what table to search on).
    To specify search terms, you will need to create a predicate (otherwise the request will simply return everything in the table). You can also specify a set of sort descriptors so that your results will be sorted.

    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"table" inManagedObjectContext:context];
    [request setEntity:entity];
    
    NSError *errorFetch = nil;
    NSArray *array = [context executeFetchRequest:request error:&errorFetch];
    

    This code creates a fetch request, and returns every object from the table named “table” in an array.
    From here, since all of the required objects are in the array, you can inspect and edit the records. If you make any changes, remember to save the context!
    The following loop logs the first value in each object, using the same table as the above examples.

    for(TableObject *object in array)
    {
        NSLog(@"object value1 = %@", object.value1);
    }
    

    You can also delete records from this point as well using the above mentioned function.

    For more information about Fetch Requests, please give the class reference a look. I would also highly recommend reading about sort descriptors and predicates, since they’re very important for searching your Core Data db, and certain uses of them are less efficient than others (particularly in the creation of NSPredicates).

    Good luck!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I want to construct a data frame in an Rcpp function, but when I
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and

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.