NSArray subsets and sorting from a dynamic parentArray
I've run into a big question of an approach in order to fuel a graph view.
I retrieve Arrays via coredata.
A Parent Parent <--->>Entry entity relationship exists.
Code:
Parent <-------->>Entry
I need to combine the results of parent <-->> Entry relationship into an array sorted by NSDate, thats pretty easy, a parent could have multiple Entries based on timeStamp (NSDate) of adding it.
Two points to note here,
1- Array Result_ParentA has no entryObject on date "4th" and likewise, B array has no entry on "5th", lets assume those digits to be their NSDates of adding.
I want to have an array that gives me a result as an Array like:
Code:
CombinedSortedArray objectAtIndex:0] = Array{A0, B0, C0} CountRemains 3
CombinedSortedArray objectAtIndex:1] = Array{A1, B1, C1} Count remains 3
CombinedSortedArray objectAtIndex:4] = Array{0.0, B4, C4}
CombinedSortedArray objectAtIndex:5] = Array{A5, 0.0, C5}
//Count of all the subset Arrays remains 3 which is equal to the count of Parents! Only the difference being that a nil result is replaced by 0.0!
2- All this is more complicated by knowing that the Parent is a dynamic number, I can't know how many parents are viewed in the graph..
Perhaps I should change my fetch request to give me a sorted array and do some minor changes after I get it..?
I don't know if there is a simple, elegant solution to your problem. The easiest way, though not the most efficient, is to iterate through your result arrays and create new arrays that hold the newly sorted data. There shouldn't be anything really hard in doing that, but if you have a large number of Parents and Entries then things can slow down and you need to look at ways to optimize. The good thing is that I believe you should be able to implement everything in a single pass through the data.
Since im fetching this via coredata,
Fetch each parent separately,
Since the parent count is itself dynamic, I'd be iterating to fetch their arrays.
Code:
NSMutableArray ParentEntriesArray;
for (i=0; i<parentsArray; i++){
Parent *parent = [parentsArray objectAtIndex:i];
[parentEntriesArray addObject:parent.entries];
//Parent is nsmanagedobject, im just calling parent.entries
//parent has entries as to-many relationship with Entry entity.
}
Im confused after this?? I need to iterate over this array three times each and then grab the individual objects which could that could be at any index and at the same time, check for timeStamp, ie, if if C is not available at 1st,then just add 0.0!
theres strict math here.. i guess, please help me out.