Question concerning NSPredicate


#1

Found this weird. When I put the call to give up ownership of an employee object from the employee array before the NSPredicate is set up, it works fine.

        NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets" ascending:YES];
        NSSortDescriptor *ei = [NSSortDescriptor sortDescriptorWithKey:@"EmployeeID" ascending:YES];
        [employeeArray sortUsingDescriptors:[NSArray arrayWithObjects:voa,ei, nil]];
        
        NSLog(@"Employees: %@",employeeArray);
    
        
        NSLog(@"giving up owenership of an employee");
        [employeeArray removeObjectAtIndex:3];
        
        NSLog(@"allAssets: %@",allAssets);
        
        
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
        NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
        NSLog(@"toBeReclaimed: %@",toBeReclaimed);
        toBeReclaimed = nil;
        
        NSLog(@"empty the whole array");
        allAssets = nil;
        employeeArray = nil;
        [/code]

and that prints:

[code]2013-07-30 01:09:55.767 BMITime[824:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 9: $136 in assets>",
    "<Employee 1: $153 in assets>"
)
2013-07-30 01:09:55.769 BMITime[824:303] giving up owenership of an employee
2013-07-30 01:09:55.770 BMITime[824:303] deallocating <Employee 7: $34 in assets>
2013-07-30 01:09:55.770 BMITime[824:303] allAssets: (
    "< Laptop 0: $0, assigned to <Employee 3: $68 in assets> >",
    "< Laptop 1: $17, assigned to <Employee 6: $119 in assets> >",
    " <Laptop 2: $34>",
    "< Laptop 3: $51, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 4: $68, assigned to <Employee 3: $68 in assets> >",
    "< Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 7: $119, assigned to <Employee 2: $119 in assets> >",
    "< Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
    "< Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2013-07-30 01:09:55.772 BMITime[824:303] toBeReclaimed: (
    "< Laptop 1: $17, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 3: $51, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 7: $119, assigned to <Employee 2: $119 in assets> >",
    "< Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
    "< Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2013-07-30 01:09:55.772 BMITime[824:303] empty the whole array
2013-07-30 01:09:55.773 BMITime[824:303] deallocation  <Laptop 2: $34>
2013-07-30 01:09:55.774 BMITime[824:303] deallocating <Employee 0: $0 in assets>
2013-07-30 01:09:55.774 BMITime[824:303] deallocating <Employee 4: $0 in assets>
2013-07-30 01:09:55.775 BMITime[824:303] deallocating <Employee 8: $0 in assets>
2013-07-30 01:09:55.775 BMITime[824:303] deallocating <Employee 1: $153 in assets>
2013-07-30 01:09:55.776 BMITime[824:303] deallocation  <Laptop 9: $153>
2013-07-30 01:09:55.776 BMITime[824:303] deallocating <Employee 9: $136 in assets>
2013-07-30 01:09:55.777 BMITime[824:303] deallocation  <Laptop 8: $136>
2013-07-30 01:09:55.777 BMITime[824:303] deallocating <Employee 2: $119 in assets>
2013-07-30 01:09:55.778 BMITime[824:303] deallocation  <Laptop 7: $119>
2013-07-30 01:09:55.778 BMITime[824:303] deallocating <Employee 5: $136 in assets>
2013-07-30 01:09:55.779 BMITime[824:303] deallocation  <Laptop 3: $51>
2013-07-30 01:09:55.779 BMITime[824:303] deallocation  <Laptop 5: $85>
2013-07-30 01:09:55.780 BMITime[824:303] deallocating <Employee 6: $119 in assets>
2013-07-30 01:09:55.780 BMITime[824:303] deallocation  <Laptop 1: $17>
2013-07-30 01:09:55.781 BMITime[824:303] deallocation  <Laptop 6: $102>
2013-07-30 01:09:55.781 BMITime[824:303] deallocating <Employee 3: $68 in assets>
2013-07-30 01:09:55.781 BMITime[824:303] deallocation  <Laptop 0: $0>
2013-07-30 01:09:55.782 BMITime[824:303] deallocation  <Laptop 4: $68>[/code]



but if I put the call to remove an employee object from its array after the predicate is set up:

[code]
        NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets" ascending:YES];
        NSSortDescriptor *ei = [NSSortDescriptor sortDescriptorWithKey:@"EmployeeID" ascending:YES];
        [employeeArray sortUsingDescriptors:[NSArray arrayWithObjects:voa,ei, nil]];
        
        NSLog(@"Employees: %@",employeeArray);
    
        
        NSLog(@"allAssets: %@",allAssets);
        
        
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
        NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
        NSLog(@"toBeReclaimed: %@",toBeReclaimed);
        toBeReclaimed = nil;
        
        NSLog(@"giving up owenership of an employee");
        [employeeArray removeObjectAtIndex:3];

        
        NSLog(@"empty the whole array");
        allAssets = nil;
        employeeArray = nil;
[/code]


this is what logs to the console. [code]
2013-07-30 01:12:18.883 BMITime[831:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 9: $136 in assets>",
    "<Employee 1: $153 in assets>"
)
2013-07-30 01:12:18.886 BMITime[831:303] allAssets: (
    "< Laptop 0: $0, assigned to <Employee 3: $68 in assets> >",
    "< Laptop 1: $17, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 2: $34, assigned to <Employee 7: $34 in assets> >",
    "< Laptop 3: $51, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 4: $68, assigned to <Employee 3: $68 in assets> >",
    "< Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 7: $119, assigned to <Employee 2: $119 in assets> >",
    "< Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
    "< Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2013-07-30 01:12:18.887 BMITime[831:303] toBeReclaimed: (
    "< Laptop 1: $17, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 3: $51, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
    "< Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
    "< Laptop 7: $119, assigned to <Employee 2: $119 in assets> >",
    "< Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
    "< Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2013-07-30 01:12:18.887 BMITime[831:303] giving up owenership of an employee
2013-07-30 01:12:18.888 BMITime[831:303] empty the whole array
2013-07-30 01:12:18.888 BMITime[831:303] deallocating <Employee 0: $0 in assets>
2013-07-30 01:12:18.888 BMITime[831:303] deallocating <Employee 4: $0 in assets>
2013-07-30 01:12:18.889 BMITime[831:303] deallocating <Employee 8: $0 in assets>
2013-07-30 01:12:18.889 BMITime[831:303] deallocating <Employee 1: $153 in assets>
2013-07-30 01:12:18.890 BMITime[831:303] deallocation  <Laptop 9: $153>
2013-07-30 01:12:18.891 BMITime[831:303] deallocating <Employee 9: $136 in assets>
2013-07-30 01:12:18.891 BMITime[831:303] deallocation  <Laptop 8: $136>
2013-07-30 01:12:18.891 BMITime[831:303] deallocating <Employee 2: $119 in assets>
2013-07-30 01:12:18.892 BMITime[831:303] deallocation  <Laptop 7: $119>
2013-07-30 01:12:18.892 BMITime[831:303] deallocating <Employee 5: $136 in assets>
2013-07-30 01:12:18.893 BMITime[831:303] deallocation  <Laptop 3: $51>
2013-07-30 01:12:18.893 BMITime[831:303] deallocation  <Laptop 5: $85>
2013-07-30 01:12:18.894 BMITime[831:303] deallocating <Employee 7: $34 in assets>
2013-07-30 01:12:18.894 BMITime[831:303] deallocation  <Laptop 2: $34>
2013-07-30 01:12:18.894 BMITime[831:303] deallocating <Employee 6: $119 in assets>
2013-07-30 01:12:18.895 BMITime[831:303] deallocation  <Laptop 1: $17>
2013-07-30 01:12:18.895 BMITime[831:303] deallocation  <Laptop 6: $102>
2013-07-30 01:12:18.896 BMITime[831:303] deallocating <Employee 3: $68 in assets>
2013-07-30 01:12:18.896 BMITime[831:303] deallocation  <Laptop 0: $0>
2013-07-30 01:12:18.897 BMITime[831:303] deallocation  <Laptop 4: $68>

The employee being removed never calls the dealloc method. What makes even less sense is that the employee that I used doesn’t hold more than 70 in assets so even if this toBeReclaimed array was hypothetically owning employee objects and not asset objects (which I don’t think it is) this one wouldn’t be being owned anyway and also before it’s called toBeReclaimed is set to nil so it double doesn’t make sense!

What is actually happening here?? Please help!