Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-05-2012, 07:55 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 109
raheel is on a distinguished road
Default 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.

I use a fetchRequest with the following:

Code:
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY forParent.entries == %@",selectedTrack]
Using that predicate I can take out the entries of Parent of multiple parents into different set of Arrays.

But I would like to have a combined result thats sorted Based on TimeStamps

For example,

Code:
    NSArray Result_ParentA = [entries sortedBy timeStamp]; {A0 ,A1, A2, A3,     A5}   (5)
    NSArray Result_ParentB = [entries sortedBy TimeStamp]; {B0, B1, B2, B3, B4}       (5)
    NSArray Result_ParentC = [entries sortedBy TimeStamp]; {C0, C1, C2, C3, C4  C5 }  (6)
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..?

Im clueless now, any help is appreciated.
raheel is offline   Reply With Quote
Old 02-07-2012, 02:17 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

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.
fiftysixty is offline   Reply With Quote
Old 02-07-2012, 06:13 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 109
raheel is on a distinguished road
Default

Thanks for replying,

Yes, theres no elegant solution i guess..

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.
}
I now have
Code:
ParentEntryArray (3 count) { Array[a0, a1, a2, a4], 
                                        Array[b0, b1, b2],
                                        Array[c0, c2, c3] }
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.
raheel is offline   Reply With Quote
Old 02-07-2012, 08:40 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 109
raheel is on a distinguished road
Default

I kinda got it..

Code:
NSArray *pA = [NSArray arrayWithObjects:@"A0", @"A1", @"A2", @"A3", @"A4", @"A5", @"A6", nil];
NSArray *pB = [NSArray arrayWithObjects:@"B0", @"B1", @"B2", @"B3", @"B4", @"B5", @"B6", nil];
NSArray *pC = [NSArray arrayWithObjects:@"C0", @"C1", @"C2", @"C3", @"C4", @"C5", @"C6", nil];
NSMutableArray *combinedArray = [NSMutableArray arrayWithObjects:pA, pB, pC, nil];
    
NSMutableArray *sorted = [NSMutableArray new];
    NSInteger dateCount = pA.count; //dateCount will be equal number of points to show!
    int e; //entry counter of each parent aray;
    int p; //parent counter of each parent in combined array;
    
    for(e=0; e<dateCount; e++){
        NSMutableArray *combinedObjectsArray = [NSMutableArray new];
        for(p=0; p<combinedArray.count; p++){
            [combinedObjectsArray addObject:[[combinedArray objectAtIndex:p] objectAtIndex:e]];
            
        }
        [sorted addObject:combinedObjectsArray];
        //[combinedObjectArray removeAllObjects];
    }
    
    NSLog(@"%@ %d", sorted.description, sorted.count);
Gets me the desired result but only if its classical., as in, if one of the objects are missing, there shall be chaos!
raheel is offline   Reply With Quote
Reply

Bookmarks

Tags
coredata, nsarray, sorting, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
17 members and 385 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0