luca
luca

Reputation: 37146

Fetch NSManagedObject with specific property

Hi I have a NSArray (playersList) containing the following NSManagedObjects :

(
    "<NSManagedObject: 0x6b75980> (entity: Players; id: 0x6b749d0 <x-coredata://463316AB-BCF5-4257-AE5B-26E3AAB0DAE1/Players/p1> ; data: { \"id_player\" = 2;\n    name = 7777;\n})",
    "<NSManagedObject: 0x6b759e0> (entity: Players; id: 0x6b749e0 <x-coredata://463316AB-BCF5-4257-AE5B-26E3AAB0DAE1/Players/p2> ; data: { \"id_player\" = 3;\n    name = \"hcp 5\";\n})",
    "<NSManagedObject: 0x6b756d0> (entity: Players; id: 0x6b749c0 <x-coredata://463316AB-BCF5-4257-AE5B-26E3AAB0DAE1/Players/p3> ; data: {\"id_player\" = 4;\n    name = 1;\n})"
)

How can I query it to return the object with 'id_player==3'?

Upvotes: 1

Views: 381

Answers (2)

JeremyP
JeremyP

Reputation: 86691

There are several ways to do it.

  • iterate the array and just check the id_player property of each object in the array

    NSIndexSet* indexes = [originalArray indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop)
                          {
                              return [obj id_player] == 3;
                          }];
    
  • use a predicate (see the predicate programming guide for details)

    NSPredicate* predicate = [NSPredicate predicateWithFormat: @"id_player == %d", 3];
    NSArray* results = [originalArray filteredArrayUsingPRedicate: predicate];
    

Upvotes: 0

JAHelia
JAHelia

Reputation: 7932

create a fetch request, and fill it with the desired EntityDescription name, and a NSPredicate object which contains the condition id_player == 3, then make excuteFetchRequest to run that request against the management model.

Upvotes: 1

Related Questions