0

I have an NSArray of objects and the data looks like this. I am currently trying to sum up the value of hours in the timeSheets property of the task given. I currently have a double for loop that does the job for the iPhone however it runs into a performance issue with lag on a UITableView on the iPad version. The code for the double for loop is at the bottom. I'm trying to use a mixture of NSExpressions and NSPredicates however, I cant seem to get these to add correctly.

     <Tasks: 0xc563950> (entity: Tasks; id: 0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8> ; data: {
    assigneeID = 20;
    budgetedHours = 5;
    chargeNumber = 0;
    completedProgress = 0;
    createdBy = "Tyler Baetz";
    dateCreated = "2013-09-06 19:18:40 +0000";
    dateModified = "2013-09-11 04:53:32 +0000";
    descriptions = nil;
    dueDate = "2013-09-11 07:00:00 +0000";
    modifiedBy = "Jose Alvarez";
    name = Denver;
    ownerID = 120;
    parentID = 1914;
    project = "0xc566070 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Projects/p2>";
    projectID = 500910;
    taskID = 1916;
    taskPriority = nil;
    taskPriorityID = 1;
    taskStatus = nil;
    taskStatusID = 3;
    taskType = nil;
    taskTypeID = 4;
    tasks = "<relationship fault: 0xb4e9db0 'tasks'>";
    timeSheets =     (
        "0xc553780 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p12>",
        "0xc5537a0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p14>",
        "0xc5537b0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p15>"
    );
}) with objects {(
    <TimeSheets: 0xc555420> (entity: TimeSheets; id: 0xc553780 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p12> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-06 20:31:31 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 20;
    hours = 173;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-01 07:00:00 +0000";
    timeSheetID = 1934;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555730> (entity: TimeSheets; id: 0xc5537a0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p14> ; data: {
    approvedBy = nil;
    comments = "Friday :45";
    dateApproved = nil;
    dateEntered = "2013-09-06 20:31:31 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 20;
    hours = 45;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-06 07:00:00 +0000";
    timeSheetID = 1937;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5557a0> (entity: TimeSheets; id: 0xc5537b0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p15> ; data: {
    approvedBy = nil;
    comments = "Hello Man";
    dateApproved = nil;
    dateEntered = "2013-09-11 13:20:19 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 50;
    hours = 300;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-05 07:00:00 +0000";
    timeSheetID = 2357;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
})
)},
Relationship 'timeSheets' on managed object (0xc5639f0) <Tasks: 0xc5639f0> (entity: Tasks; id: 0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9> ; data: {
    assigneeID = 20;
    budgetedHours = 3;
    chargeNumber = 0;
    completedProgress = 0;
    createdBy = "Tyler Baetz";
    dateCreated = "2013-09-06 19:17:01 +0000";
    dateModified = "2013-09-12 18:34:55 +0000";
    descriptions = nil;
    dueDate = "2013-09-09 07:00:00 +0000";
    modifiedBy = "Jose Alvarez";
    name = "Jose's Task";
    ownerID = 120;
    parentID = 1913;
    project = "0xc566070 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Projects/p2>";
    projectID = 500910;
    taskID = 1915;
    taskPriority = nil;
    taskPriorityID = 4;
    taskStatus = nil;
    taskStatusID = 3;
    taskType = nil;
    taskTypeID = 4;
    tasks = "<relationship fault: 0xb4ee0d0 'tasks'>";
    timeSheets =     (
        "0xc5537c0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p16>",
        "0xc553810 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p21>",
        "0xc553800 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p20>",
        "0xc553790 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p13>",
        "0xc5537d0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p17>"
    );
}) with objects {(
    <TimeSheets: 0xc5557f0> (entity: TimeSheets; id: 0xc5537c0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p16> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-11 17:05:11 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 205;
    reportPeriod = nil;
    reportPeriodID = 5;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-11 17:05:18 +0000";
    timeSheetID = 2633;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5559b0> (entity: TimeSheets; id: 0xc553810 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p21> ; data: {
    approvedBy = nil;
    comments = Rarer;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:05:11 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-19 07:00:00 +0000";
    timeSheetID = 3297;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555950> (entity: TimeSheets; id: 0xc553800 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p20> ; data: {
    approvedBy = nil;
    comments = Cbfb;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:33:05 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-16 07:00:00 +0000";
    timeSheetID = 3298;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5556c0> (entity: TimeSheets; id: 0xc553790 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p13> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-12 18:34:55 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 284;
    reportPeriod = nil;
    reportPeriodID = 5;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-12 18:21:59 +0000";
    timeSheetID = 2827;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555850> (entity: TimeSheets; id: 0xc5537d0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p17> ; data: {
    approvedBy = nil;
    comments = Asdad;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:03:32 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-15 07:00:00 +0000";
    timeSheetID = 3296;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
})
)}
)

Code For Double For Loop:

- (NSNumber *) getSumOfChargedHoursForAnArrayOfTasks: (NSArray *) tasksArray
{
    NSArray *timeSheets = [tasksArray valueForKeyPath:@"timeSheets"];

    NSNumber *chargedHours = [NSNumber numberWithInt:0];

    for(int i = 0; i < [timeSheets count]; ++i)
    {
        NSArray *innerTimeSheet = [timeSheets objectAtIndex:i];

        for(int j = 0; j < [innerTimeSheet count]; ++j)
        {
            NSNumber *innerNumber = [innerTimeSheet valueForKeyPath:@"@sum.hours"];

            chargedHours = [NSNumber numberWithFloat:[chargedHours intValue] + [innerNumber intValue]];
        }

    }

    return chargedHours;
}
4

1 回答 1

2

如果您尝试对一组任务中所有考勤表中的小时数求和,则应考虑valueForKeyPath:按以下方式使用:

[tasks valueForKeyPath:@"@sum.timeSheets.@sum.hours"]

现在看来,您将每个任务的小时总和乘以timeSheets每个任务的时间表数量。

于 2013-09-20T21:13:47.907 回答