获取核心数据中特定属性内的所有记录?

时间:2017-05-26 06:59:04

标签: ios core-data

在这个例子中,我有一个名为“nameArray”的数组,它包含五个对象 - {A,B,C,D,E}。在Core Data中,我有一个名为“Details”的实体和一个属性“name”。我已将“nameArray”保存在核心数据库中。之后我使用NSFetchRequest来获取存储在“name”属性中的所有数据。我使用的代码如下:

_nameArray = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E", nil];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
Details *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:appDelegate.persistentContainer.viewContext];
for (NSString*name1 in _nameArray)
{
    userDetails.name = name1;
}
NSError *error;
[appDelegate.persistentContainer.viewContext save:&error];
if (error==nil)
{
    NSLog(@"Data is saved");
}
else
{
    NSLog(@"Data not saved and error is %@",error);
}

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Details"];
NSManagedObjectContext *context = [self manage]
NSArray *results = [appDelegate.persistentContainer.viewContext executeFetchRequest:request error:nil];
NSLog(@"The result array is %@",results);
if (results.count>0)
{
    NSManagedObject *name1 = (NSManagedObject *)[results objectAtIndex:0];
    NSLog(@"The name is %@",[name1 valueForKey:@"name"]);
}

问题是我无法获取存储在“name”属性中的所有记录。 “results”数组只给出一个对象,即最后一个对象'E'作为输出。我需要输出存储在“name”属性中的所有数据。 “结果”数组输出如下: -

  

结果数组是(       “(entity:Details; id:0xd000000000040000; data :)),       “(entity:Details; id:0xd000000000080000; data:{\ n name = E; \ n number = 0; \ n})”   )

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法获取特定实体的所有记录。

在你的情况下,将nil传递给谓词

+(NSArray*)getAllObjectsForEntityName:(NSString*)entityName WithPredicate:(NSPredicate*)predicate{


    AppDelegate *appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;


    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:entityName];

    NSError *error = nil;
    if(predicate)
       [request setPredicate:predicate];

    NSArray *results = [appDel.managedObjectContext executeFetchRequest:request error:&error];

    if (error != nil) {

        //Deal with failure
    }
    else {

        //Deal with success
    }
    return results;
}

要在核心数据中保存数据,您必须更新代码

_nameArray = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E", nil];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

for (NSString*name1 in _nameArray)
{
Details *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:appDelegate.persistentContainer.viewContext];
    userDetails.name = name1;
}

答案 1 :(得分:0)

您将一个对象插入CoreData并继续更新该对象。你需要移动线

Details *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:appDelegate.persistentContainer.viewContext];

进入for循环如下

for (NSString*name1 in _nameArray)
{
    Details *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:appDelegate.persistentContainer.viewContext];
    userDetails.name = name1;
}

如果你想看到所有插入的对象

NSArray *results = [appDelegate.persistentContainer.viewContext executeFetchRequest:request error:nil];
if (results.count>0)
{
    for (NSManagedObject*userDetails in results)
    {
      NSLog(@"The name is %@",[userDetails valueForKey:@"name"]);
    }
}