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 8441609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:36:22+00:00 2026-06-10T08:36:22+00:00

I have a coredata project that I’m trying to programmatically update a number. I’m

  • 0

I have a coredata project that I’m trying to programmatically update a number.
I’m retrieving objects from CoreData and then storing it into an array.

Then, I’m looping through that array to see if the current user’s IP is present in the database and trying to update the number of times accessed for that specific array.

The problem is, it’s updating all the objects, not just the current object in the looped array.

First, I get the info from core data like so:

- (void)fetchRecords {

    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"IPAddr" inManagedObjectContext:managedObjectContext];

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ipDate" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];


    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }

    // Save our fetched data to an array
    [self setIpArray: mutableFetchResults];

}

Now, I’m trying to find if the current User IP is present in the fetched results, and if it’s present, update the number of times accessed:

// see if the ip is present and update if necessary
-(void)ipPresent {
    NSString * theCurrentIP = [self getGlobalIPAddress];
    for (IPAddr *allips in ipArray)
    {
        if ([allips.ipNum isEqualToString:theCurrentIP]) {
            NSLog(@"The IP %@ was found.", theCurrentIP);

            // update the ip

            NSError *error = nil;

            NSNumber *ipToUpdate = allips.ipAccess;

            NSNumber *addIpAccess = [[NSNumber alloc] initWithInt:1];
            NSNumber *updateIpAddress = [NSNumber numberWithFloat:([ipToUpdate floatValue] + [addIpAccess floatValue])];

            [self.ipArray setValue:updateIpAddress forKey:@"ipAccess"];


            if ([self.managedObjectContext save:&error]) {  // write to database
            NSLog(@"The IP Was Updated from %@ to %@", ipToUpdate, updateIpAddress);
            } else if (![self.managedObjectContext save:&error]) {
                NSLog(@"failed with error: %@", error);
            }
            break;

        } else {
            NSLog(@"The IP %@ was NOT found.", theCurrentIP);

        }
    }
} 

I’m pretty sure the issue is with this line:

[self.ipArray setValue:updateIpAddress forKey:@"ipAccess"];

Again, it’s updating ALL the entities and not just the one in the current loop.

  • 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-06-10T08:36:23+00:00Added an answer on June 10, 2026 at 8:36 am

    Indeed. You are using the wrong method. self.ipArray is a NSMutableArray.

    The method

    - (void)setValue:(id)value forKey:(NSString *)key
    

    is used for Key-Value Coding (which is what makes it work for Core Data objects), but when applied to an array, it will invoke setValue:forKey: on each entry in the array.

    Now, you can see that you could also call setValue:forKey on the one single array element allips since its property is obviously KVC compliant — otherwise you would be having a different problem, not see the values being set.

    Note, that you could also just assign the property…

    allips.ipAccess = updateIpAddress;
    

    EDIT

    Sorry, probably should have read slower… You do understand that you don’t have to use a mutable array, right? You are not actually changing the array, just the elements in the array. An immutable collection means that the collection contents can not change, but when you have a pointer to an object, as long as that object is not immutable, you can still mutate its properties.

    Thus, if you had an immutable array of Foo objects, you could do this…

    for (Foo *foo in myImmutableArray) {
        Bar *bar = [self getSomeNewBar];
        [foo setBar:bar];
    
        // If Foo is KVC compliant, you can do this too...
        [foo setValue:bar for Key:@"bar"];
    }
    

    If, however, you call setValue:forKey on the array, it will be invoked for each element of the array. Note, that setValue:forKey is actually declared in the immutable NSArray.

    EDIT

    That comment was hard to read.

    The core data object is just another object. It looks like you have subclassed it, and provided it with properties for the attributes. Just replace

    [self.ipArray setValue:updateIpAddress forKey:@"ipAccess"];
    

    with

    [allips setValue:updateIpAddress forKey:@"ipAccess"];
    

    or

    allips.ipAccess = updateIpAddress;
    

    Either of those should modify your core data object, as they would any object that had a read/write property named “ipAccess”

    Assuming, of course, that I didn’t read it wrong again… and allips is your core data object…

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

Sidebar

Related Questions

I have a project that uses coredata and i am attmepting to delete from
i have a well pre-populated sqlite file that i copied into my project (in
I have a project that uses CoreData on iOS and we have gone through
I have an existing project that I am adding the CoreData framework to but
I have an old Xcode project which contains a CoreData model (containing a version
In this test Core Data project, I have a one to many relationship from
I have an application that uses CoreData. I previously had a class named Marker
I am currently working on a project that uses CoreData and relations are using
I am using CoreData for an iPhone project and I am stuck trying to
We have a project that has two one to many relationships. We encounter a

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.