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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:29:21+00:00 2026-05-28T07:29:21+00:00

I am a newbie and I would like to store and retrieve data from

  • 0

I am a newbie and I would like to store and retrieve data from a Core Data database.
I get all the data from a php file, which communicates with a SQL database. This php file returns a JSON object, which in turn is parsed by my app and then written into Core Data by doing the following:

AppDelegate.m

-(void)writeDataIntoCoreData
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *articles = [NSEntityDescription
                                 insertNewObjectForEntityForName:@"Articles" 
                                 inManagedObjectContext:context];

    for(int i= 0; i<[json count];i++){
        NSDictionary *info = [json objectAtIndex:i];

        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber * myNumber = [f numberFromString:[info objectForKey:@"userID"]];

        [articles setValue:myNumber forKey:@"articleID"];
        [articles setValue:[info objectForKey:@"username"] forKey:@"author"];
        [articles setValue:[info objectForKey:@"user_pic"] forKey:@"text"];
        NSLog(@"Name = %@",[info objectForKey:@"username"]);
        NSLog(@"Text = %@",[info objectForKey:@"user_pic"]);
    }
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }


}

MainViewController.m

Here I try to retrieve all the data in the Core Data database by iterating through the fetchedObjects.

-(IBAction)showCD:(id)sender
{

    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Articles" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *info in fetchedObjects) {
        NSLog(@"id: %@", [info valueForKey:@"articleID"]); 
        NSLog(@"Name: %@", [info valueForKey:@"author"]);
        NSLog(@"Text: %@", [info valueForKey:@"text"]);


    }    

}

The NSLog, however, only displays one object, namely the first one that was written into the database. What is wrong ?

  • 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-28T07:29:21+00:00Added an answer on May 28, 2026 at 7:29 am

    move your

    NSManagedObject *articles = [NSEntityDescription
                                 insertNewObjectForEntityForName:@"Articles" 
                                 inManagedObjectContext:context];
    

    into your for loop,

    for(int i= 0; i<[json count];i++){
        NSManagedObject *articles = [NSEntityDescription
                                 insertNewObjectForEntityForName:@"Articles" 
                                 inManagedObjectContext:context];
        NSDictionary *info = [json objectAtIndex:i];
    
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber * myNumber = [f numberFromString:[info objectForKey:@"userID"]];
    
        [articles setValue:myNumber forKey:@"articleID"];
        [articles setValue:[info objectForKey:@"username"] forKey:@"author"];
        [articles setValue:[info objectForKey:@"user_pic"] forKey:@"text"];
        NSLog(@"Name = %@",[info objectForKey:@"username"]);
        NSLog(@"Text = %@",[info objectForKey:@"user_pic"]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am from a programming background ,and newbie to flex3 . i would like
I have millions of lines generated from data updated every second which look like
I am a newbie to R. I would like to create a barplot which
I'm a newbie in WPF and I would like to ask if it's possible
StructureMap newbie question. public class SomeClass: IInterface1, IInterface2 { } I would like the
I'm kind of a newbie with PHP so please bear with me. I would
I would like add a this Apple example [AccelerometerGraph] the ability to store the
I would like to know as a newbie programmer what the benefits are of
I store rdl's remotely in a sql server database in binary format. I would
I'm an iPhone/iPod-touch newbie, and would like to write an iPhone application utilizing the

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.