Objective-C Getter Memory Management
Posted
by Marian André
on Stack Overflow
See other posts from Stack Overflow
or by Marian André
Published on 2010-05-16T21:21:03Z
Indexed on
2010/05/16
21:30 UTC
Read the original article
Hit count: 319
objective-c
|memory-management
I'm fairly new to Objective-C and am not sure how to correctly deal with memory management in the following scenario:
I have a Core Data Entity with a to-many relationship for the key "children". In order to access the children as an array, sorted by the column "position", I wrote the model class this way:
@interface AbstractItem : NSManagedObject
{
NSArray * arrangedChildren;
}
@property (nonatomic, retain) NSSet * children;
@property (nonatomic, retain) NSNumber * position;
@property (nonatomic, retain) NSArray * arrangedChildren;
@end
@implementation AbstractItem
@dynamic children;
@dynamic position;
@synthesize arrangedChildren;
- (NSArray*)arrangedChildren
{
NSArray* unarrangedChildren = [[self.children allObjects] retain];
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
[arrangedChildren release];
arrangedChildren = [unarrangedChildren sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
[unarrangedChildren release];
return [arrangedChildren retain];
}
@end
I'm not sure whether or not to retain unarrangedChildren and the returned arrangedChildren (first and last line of the arrangedChildren getter). Does the NSSet allObjects method already return a retained array? It's probably too late and I have a coffee overdose.
I'd be really thankful if someone could point me in the right direction. I guess I'm missing vital parts of memory management knowledge and I will definitely look into it thoroughly.
© Stack Overflow or respective owner