web-dev-qa-db-ja.com

NSManagedObjectSubClassを使用してCoreDataBaseからすべてのレコードを取得するにはどうすればよいですか?

CoreDataアプリケーションにiosを実装しようとしています。次に、エンティティ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 ");
       }      
}

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イベント(MUSTAHFAのインスタンス)データベースからすべてのレコードをフェッチするにはどうすればよいですか?

13
Musthafa
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];
29
Joseph DeCarlo
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
}
9
Ishwar Hingu