获取coredata中的数据

时间:2014-04-01 06:46:36

标签: ios iphone core-data ios7

-(void)getRecords
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CustomerOrder"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError* error;

       NSArray *fetchedRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];


    if (fetchedRecords)
    {
        if (fetchedRecords.count == 0) //create new Entity
        {
            NSLog(@"Balaiah babu");
        }

        else
        {
            int count =0;
            for (int i=0; i<fetchedRecords.count; i++) {


                CustomerOrder * record = [fetchedRecords objectAtIndex:i];
                NSLog(@"%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@",record.customer_id,record.order_id,record.outletLatitude,record.outletLongitude,record.trigger_days,record.trigger_end_date,record.trigger_from_time,record.trigger_no_days,record.trigger_radius,record.trigger_start_date,record.trigger_to_time);

                NSLog(@"%d",count++);

            }
        }

    }

}

这里获取的记录数组显示为nil.But它正在循环并打印log的值。我不知道它是如何发生的。

1 个答案:

答案 0 :(得分:0)

在.h文件中

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

在.m文件中

获取实体“信息”

NSEntityDescription *entity = 
 [NSEntityDescription entityForName:@"Info" 
             inManagedObjectContext:managedObjectContext];

获取实体请求

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:entity];

获取记录并处理错误

NSMutableArray *mutableFetchResults = 
 [[managedObjectContext executeFetchRequest:request 
                                      error:&error] mutableCopy];

if (!mutableFetchResults)
    {
    Nslog(@"nil");
}

将我们提取的数据保存到数组

NSLog(@"get_Result==%@",[mutableFetchResults valueForKey:@"fname"]);