user1227928
user1227928

Reputation: 829

Creating new array from a core data array object?

i am currently working on Core Data and i have following code

NSManagedObjectContext *managedObjContxt = [self managedObjectContext];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc] initWithEntityName:@"BDEvent"];
NSMutableArray *fetchedObj= [[managedObjContxt executeFetchRequest:fetchReq error:nil] mutableCopy];

Now my fetchedObj array contains

<_PFArray 0x7b78d4e0>(
<BDEvent: 0x7b78cf40> (entity: BDEvent; id: 0x7b78be60 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p1> ; data: {
    name = rock;
    status = "D1";
    timestamp = "18-02-2015 12:10:10";
}),
<BDEvent: 0x7b78d1f0> (entity: BDEvent; id: 0x7b78be70 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p2> ; data: {
    name = Illa;
    status = "D1";
    timestamp = "18-02-2015 12:15:01";
}),
<BDEvent: 0x7b78d290> (entity: BDEvent; id: 0x7b78be80 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p3> ; data: {
    name = john;
    status = "EXIT|D2";
    timestamp = "18-02-2015 12:25:05";
})
)

Now i want to create 2 array based on status

Array1 will contain timestamp value which does not have EXIT status and Array2 will contain timestamp value which does have EXIT status.

How to achieve this ?

Upvotes: 0

Views: 48

Answers (1)

Rukshan
Rukshan

Reputation: 8066

To get an array with 'Exit' code , add a predicate to your fetch request like this.

    NSManagedObjectContext *managedObjContxt = [self managedObjectContext];
    NSFetchRequest *fetchReq = [[NSFetchRequest alloc] initWithEntityName:@"BDEvent"];

//add this line
    fetchReq.predicate = [NSPredicate predicateWithFormat:@"status contains 'EXIT'"];


    NSMutableArray *fetchedObj= [[managedObjContxt executeFetchRequest:fetchReq error:nil] mutableCopy];

to get an array without 'EXIT' code, use following predicate,

  fetchReq.predicate = [NSPredicate predicateWithFormat:@"NOT (status contains 'EXIT')"];

Upvotes: 1

Related Questions