How to get all records From CoreData Base using NSManagedObjectSubClass?
Asked Answered
S

2

13

I am trying to implement CoreData in ios Application,Now I want to Fetch all records from Entity MUSTHAFA

  My  NSManagedObjectedSubClass is MUSTAHFA

MUSTHAFA.m

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface MUSTHAFA : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * FirstName;
@property (nonatomic, retain) NSNumber * Age;
@property (nonatomic, retain) NSString * Location;
@property (nonatomic, retain) NSString * LastName;

@end

#import "MUSTHAFA.h"


@implementation MUSTHAFA
@dynamic FirstName;
@dynamic Age;
@dynamic Location;
@dynamic LastName;

@end

Adding Records to Core Data

-(void)AddRecordToCoreData{

    //NSLog(@"______ ADD Core Data Implementaion");  
    MUSTHAFA *event = (MUSTHAFA *)[NSEntityDescription insertNewObjectForEntityForName:@"MUSTHAFA" inManagedObjectContext:managedObjectContext];
    [event setLastName:@"JOHN"];
    [event setFirstName:@"JOSE "];
    [event setLocation:@"IDUKKI "];
    [event setAge:[NSNumber numberWithInt:25]];    
    NSError *error;
    if (![managedObjectContext save:&error]) 
        {       
        NSLog(@"Error..%@",error);
    }
       else
       {
        NSLog(@"Data added to MUSTHAFA ");
       }      
}

Retrieving all Values from Core Data just like select * from dbTable;

-(void)FetchRecordFromCoreData:(id)data1{

     //NSLog(@"______ Fetch Core Data Implementaion");  
    MUSTHAFA *event = (MUSTHAFA *)[NSEntityDescription insertNewObjectForEntityForName:@"MUSTHAFA" inManagedObjectContext:managedObjectContext];

   //USING event how can fetch all record from Data base;


}

USING event (Instance of MUSTAHFA) how can fetch all record from Data base?

Southward answered 24/4, 2012 at 12:10 Comment(0)
A
30
NSManagedObjectContext *context = //Get it from AppDelegate

NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"MUSTHAFA"];

NSError *error = nil;

NSArray *results = [context executeFetchRequest:request error:&error];

if (error != nil) {

   //Deal with failure
}
else {

   //Deal with success
}

[request release];
Antevert answered 24/4, 2012 at 12:27 Comment(3)
what is NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"MUSTHAFA"]; line meansSouthward
That's what it does. If you are wanting to use CoreData, I suggest you go through a tutorial. Here's one: raywenderlich.com/934/…Antevert
https://mcmap.net/q/903332/-display-coredata-records/…Dennet
S
9
AppDelegate *delegate = [UIApplication sharedApplication].delegate;

NSManagedObjectContext *context =[delegate managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Channels"];

NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];

ChannelDBArray=[[NSMutableArray alloc]init];

for (NSManagedObject *obj in results) {


    NSArray *keys = [[[obj entity] attributesByName] allKeys];
    NSDictionary *dictionary = [obj dictionaryWithValuesForKeys:keys];

}
if (error != nil) {

    //Deal with failure
}
else {

    //Deal with success
}
Seigel answered 6/1, 2016 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.