Как получить все записи с базы CoreData с помощью NSManagedObjectSubClass?

Я пытаюсь реализовать CoreData в ios приложении, теперь я хочу получить все записи из 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

Добавление записей в основные данные

-(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 ");
       }      
}

Извлечение всех значений из основных данных так же, как выбор * из 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;


}

ИСПОЛЬЗОВАНИЕ события (экземпляр MUSTAHFA), как можно получить всю запись из базы данных?

Ответ 1

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];

Ответ 2

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
}