How to configure predicate to return only 2 columns?

Asked

Viewed 50 times

4

Setting:

Query using Coredata for records with a city and state, and return only the columns 'name' and 'address' of the location.

I am currently using the following query:

// Array that will be returned

NSMutableArray* arrayRegistrosFiltrados = [[NSMutableArray alloc] init];
NSManagedObjectContext * context = [self managedObjectContext];

NSError* error;

`// Instancia objeto que irá fazer a requisiçao
NSFetchRequest* requisicao = [[NSFetchRequest alloc]initWithEntityName:entidadeEstacionamento];
NSPredicate* argumentosBusca = [NSPredicate predicateWithFormat:@"estado==%@ AND cidade==%@",estado,cidade];

[requisicao setPredicate:argumentosBusca];

NSArray*registrosTemp = [context executeFetchRequest:requisicao error:&error];`

But this way returns all the columns of the found records.

1 answer

3


The class NSFetchRequest has a property called propertiesToFetch.

You can use it as follows:

...

[requisicao setPredicate:argumentosBusca];
[requisicao setPropertiesToFetch:[NSArray arrayWithObjects:@"nome", @"endereco", nil];

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.