开发者

preparing a core data result set for a grouped uitableview

i've got a NSMutableArray created from a data source object

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

[self setAmountArray: mutableFetchResults];

every object in my mutable array has the two instance variables name and timeadded.

now i want to display all names in a uitableview grouped by the day they are added. for that i wrote the following method

-(NSMutableArray*)arrangeTransfersByDate:(NSMutableArray*)transferArray {

    // Setting up objects for this method
    NSDate *oldDate = [NSDate dateWithTimeIntervalSince1970:0.0f];
    NSDateFormatter *dateComparisonFormatter = [[NSDateFormatter alloc] init];
    [dateComparisonFormatter setDateFormat:@"yyyy-MM-dd"];
    NSMutableArray *returnArray = [[NSMutableArray alloc] init];

    for(transfers *transfer in transferArray) {

        if( [[dateComparisonFormatter stringFromDate:[transfer timeadded]] isEqualToString:[dateComparisonFormatter stringFromDate:oldDate]] ) {
            if([returnArray count] == 0) {
                [returnArray addObject:[NSMutableArray arrayWithObject:transfer]];
            } else {
                [[returnArray objectAtIndex:[returnArray count]-1] addObject:transfer];
            }
        } else {
            [returnArray addObject:[NSMutableArray arrayWithObject:transfer]];
            oldDate = [transfer timeadded];
        }

    }
    //[returnArray release];
    [dateComparisonFormatter release];
    return returnArray;


}

transferArray is my amountArray where my core data objects are stored.

so this works! but

is there a better way to do this? can you give me something like a "best practise" or simply have a look if there are some memory leaks?

thanks!


edit:

the right answer was NSFetchedResultController and its sectionNameKeyPath.

however i don't wanted to store my data twice a time.

so i created the following getter method in my NSManagedObject.

- (NSString *) pubDate {

    [self willAccessValueForKey:@"pubDate"];

    NSDateFormatter *dateComparisonFormatter = [[NSDateFormatter alloc] init];
    [dateComparisonFormatter setDateFormat:@"dd.MM.YYYY"];
    NSString *temp = [dateComparisonFormatter stringFromDate:[self pubTime]];
    [dateComparisonFormatter release];

    [sel开发者_运维百科f didAccessValueForKey:@"pubDate"];

    return temp;
}

with this i can sort my tableviewcontroller by date using my FetchedResultController and my pubTime which is a timestamp.

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                              managedObjectContext:[self managedObjectContext] 
                                                sectionNameKeyPath:@"pubDate" 
                                                         cacheName:@"transfersRoot"];

thanks to all


Generally when you want to display the results of a Core Data fetch in a UITableView, you use an NSFetchedResultsController. You can choose the attribute by which you want to group your results by specifying a sectionNameKeyPath during initialization.

That said, provided your method works then really it's up to you whether you want to change your code around or not.

But please make sure you [returnArray autorelease] before you return it. It's a generally accepted Objective-C practice that any method without "alloc", "new", or "copy" in the name will return an autoreleased object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜