Bangalore
Bangalore

Reputation: 1580

how do retrieve data from core data table ios?

HI iam using core data in my test project. Iam able to add contents from json to core data entity 'Book' .but when i tried to retriev e data am only getting last one 'data' (total 37 data) ? I created one mutablearray 'directors' to insert each retrieved values but that also not working.

Please check my code.

 NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
    directors=[[NSMutableArray alloc]init];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Book" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *info in fetchedObjects) {
        NSLog(@"1");
        [directors addObject:[info valueForKey:@"director"]];
        NSLog(@"Name: %@", info);

    }
    NSLog(@"%@",directors);

am printing 'NSLog(@"1"); to check how many times loop working ,but and am getting output like this

2014-06-15 15:40:03.665 test[12489:1303] Name: <Book: 0x8c50580> (entity: Book; id: 0x8c66a60 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p1> ; data: <fault>)
2014-06-15 15:40:03.665 test[12489:1303] 1
2014-06-15 15:40:03.666 test[12489:1303] Name: <Book: 0x8c690d0> (entity: Book; id: 0x8c68420 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p2> ; data: <fault>)
2014-06-15 15:40:03.666 test[12489:1303] 1
2014-06-15 15:40:03.667 test[12489:1303] Name: <Book: 0x8f9c8d0> (entity: Book; id: 0x8d89870 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p3> ; data: {
    actors = "Hazel Crowney, Kiran Kumar, Shahbaaz Khan, Usha Bachani, Mohammed Iqbal Khan, Alka Verma";
    censor = U;
    director = "Arshad Yusuf Pathan";
    global = N;
    lang = Hindi;
    length = "";
    rating = 0;
    releasedate = "13th Jun 2014";
    synopsis = "The story of a racecar driver who loses his eyesight. While he suffers a setback, he learns about the true value of the relationships in his life. Through the course of the film, the hero realizes the";
    title = Unforgettable;
    trailer = "";
    type = MT;
    url = "http://cnt.in.bookmyshow.com/Events/Mobile/ET00022474.jpg?dtm=15614303";
})

current code enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 346

Answers (1)

Nikolay Mamaev
Nikolay Mamaev

Reputation: 1474

As I can see from the logs, all managed objects you've received from CoreData excepting last one are faulted. It means that these objects are instances of appropriate classes representing an entity (Book) but they are not initialized. They will be initialized automatically if you try to access some field of received record. I guess the following modification to your code should work:

for (Book *info in fetchedObjects) {
    NSLog(@"1");
    [directors addObject:info.director];
    NSLog(@"Name: %@", info);

}
NSLog(@"%@",directors);

Here Book is a class inherited from NSManagedObject that represents the entity named Book. You can generate this class for any entity by highlighting the entity in your CoreData model and selecting the Create NSManagedObject subclass item from the Editor menu.

More on faulting: Core Data Programming Gudie

Upvotes: 1

Related Questions