13

我正在尝试CoreDataios应用程序中实现,现在我想从实体中获取所有记录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;


}

USING 事件(MUSTAHFA 的实例)如何从数据库中获取所有记录?

4

2 回答 2

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];
于 2012-04-24T12:27:45.577 回答
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
}
于 2016-01-06T09:41:24.180 回答