0

I have a helper class designed to get data from Core Data and pass it back to a view controller. Here's the code:

View Controller:

@interface AnnouncementsController : UIViewController

@property (nonatomic, retain) NSMutableArray *announcements;

@end

@implementation AnnouncementsController

@synthesize announcements;

- (void)viewDidLoad
{
    AnnouncementCoreDataHelper *announcementHelper = [[AnnouncementCoreDataHelper alloc] init];
    self.announcements = [announcementHelper selectAnnouncementsWithPredicate:@"isActive = 1" sortDescriptor:@"lastUpdated" sortAscending:NO];

    [super viewDidLoad];
}

@end

And the Core Data Helper implementation:

// Select one or more announcements based on a predicate if passed in and a sort order
- (NSMutableArray*)selectAnnouncementsWithPredicate:(NSString *)predicateString sortDescriptor:(NSString *)sortBy sortAscending:(BOOL)isAscending
{
    NSError *error = nil;

    // Build the entity and request
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Announcement" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    if(predicateString)
    {
        // Set the search criteria
        NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
        [request setPredicate:predicate];
    }

    if(sortBy)
    {
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortBy ascending:isAscending];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [request setSortDescriptors:sortDescriptors];
    }

    // Perform the search
    NSMutableArray *results = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if(results == Nil)
    {
        NSLog(@"%@", error);
    }

    return results;    
}

The problem is that the Core Data Helper returns two objects in its NSMutableArray results but the View Controller's NSMutableArray announcements gets nil. What is getting lost in the passing the NSMutableArray back from the helper to the controller? If I change all the variables and results to a NSArray, everything works.

beryllium
  • 29,669
  • 15
  • 106
  • 125
Allen
  • 1,165
  • 2
  • 11
  • 23
  • `if(results == Nil)` is not quite correct, [use nil](http://stackoverflow.com/questions/5908936/iphonedifference-between-nil-nil-and-null) or just `if(results)`. – beryllium Mar 21 '12 at 19:13
  • Are you sure the `announcementHelper` object you're initializing isn't nil, right? – Ash Furrow Mar 21 '12 at 19:25
  • Updated the if(results) check and the announcementHelper isn't nil. If I step through the code, the selectAnnouncementsWithPredicate method is successfully called from the view. – Allen Mar 21 '12 at 19:35
  • If you put a break point on the `[super viewDidLoad];` line, is `self. announcements ` empty? – RLH Mar 21 '12 at 19:48
  • You have lot of memory-management issues. Put `NSLog(@"%d", [results count]);` after `results =` to see what's going on. – A-Live Mar 21 '12 at 22:14
  • Can you point out the memory management issues? – Allen Mar 22 '12 at 01:09

1 Answers1

0

I believe this was a funky issue with Xcode. I clean and reran the build and everything is working. I apologize for wasting your time and thanks for the help! I would like to know more about the memory management issues though.

Allen
  • 1,165
  • 2
  • 11
  • 23