TaskInfo taskInfo = (TaskInfo) ec.faultForGlobalID(_taskInfoGID, ec);
_totalCount = taskInfo.countResultItems().longValue();
// Task start time
// This is a demo, so we are going to replace the prime processing times with the factorial processing times
taskInfo.setStartTime(new NSTimestamp(startTime));
// For demo purposes we will use batches and EC recycling, which would be common for processing huge data sets
ERXFetchSpecification<ResultItem> fs = taskInfo.fetchSpecificationForResultItems();
// Batch iterator
ERXFetchSpecificationBatchIterator fsIterator = new ERXFetchSpecificationBatchIterator(fs, ec);
// Loop for a period of time
while (fsIterator.hasNext() && !_isStopped) {
@SuppressWarnings("unchecked")
NSArray<ResultItem> batch = fsIterator.nextBatch();
for (ResultItem resultItem : batch) {
resultItem.setWorkflowState(ResultItem.WORKFLOW_CHECKING_FACTORIAL);
performFactorialProcessing(resultItem);
resultItem.setWorkflowState(ResultItem.WORKFLOW_PROCESSING_COMPLETE);
ec.saveChanges();
_elapsedTime = System.currentTimeMillis() - startTime;
// Update progress variables
_countCompleted++;
_percentComplete = (double)(_countCompleted) / (double)_totalCount;
_status = wholeNumberFormatter.format(_countCompleted) + " numbers checked for factorial proximity";
if (_isStopped) {
break;
}
}
// Swap in a fresh EC for the next batch to help with memory management
EOEditingContext freshEC = newEditingContext();
ec.unlock();
ec = freshEC;
freshEC.lock();
fsIterator.setEditingContext(ec);
// We need to refault taskInfo into the new EC after swapping
taskInfo = (TaskInfo) ec.faultForGlobalID(_taskInfoGID, ec);
}
// Complete the stats
taskInfo.setEndTime(new NSTimestamp());
taskInfo.setWorkflowState(TaskInfo.WORKFLOW_PROCESSING_COMPLETE);
long duration = taskInfo.endTime().getTime() - taskInfo.startTime().getTime();
taskInfo.setDuration(duration);