0

I have an NSLog problem where it wont keep the results the same between loads. Its quite frustrating, but if someone could shed some light that would be really great. I'm trying to call an array from my app delegate

        NSLog(@"Selected Team 1 : %@", [[[appDelegate teamRoster]objectAtIndex:1] class]);
        NSLog(@"Selected Team 0 : %@", [[[appDelegate teamRoster]objectAtIndex:0] class]);

        NSLog(@"Selected Team : %@", [[[appDelegate teamRoster]objectAtIndex:indexPath.row] class]);
        NSLog(@"Selected Team : %@", [[appDelegate teamRoster]objectAtIndex:indexPath.row]);

and three times in a row i have these returned

2011-06-22 09:56:54.734 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team 0 : __NSCFSet
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team : __NSCFSet
2011-06-22 09:56:54.736 CoCoach[33182:207] Selected Team : {(
)}
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 0 : __NSCFSet
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : <_NSIndexPathUniqueTreeNode: 0x703e6c0>

2

2011-06-22 09:58:30.082 CoCoach[33189:207] Selected Team 1 : __NSArrayM
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team 0 : NSCFString
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team : NSCFString
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team : Koch
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team 1 : __NSArrayM
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team 0 : NSCFString
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team : __NSArrayM
2011-06-22 09:58:30.086 CoCoach[33189:207] Selected Team : (
)

3

2011-06-22 09:59:17.825 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team 0 : NSCFString
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : NSCFString
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : Smith
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 0 : NSCFString
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : _UITableViewReorderingSupport
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : <_UITableViewReorderingSupport: 0x5b3cf30>

Also when i just check to see whats in the array i get the following.

2011-06-22 09:58:30.078 CoCoach[33189:207] Team Roster Array: (
    "Koch",
    "Smith"
)

Which is exactly as expected.

What gives? Any ideas. Thanks.

Update : Function that adds the array

- (void)fetchRecords:(NSString *)teamToFind{

    teamRoster = [[NSMutableArray alloc] init];

    NSManagedObjectContext *context = [self managedObjectContext];
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teams" inManagedObjectContext:context];

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Define how we will sort the records
    NSSortDescriptor *teams = [[NSSortDescriptor alloc] initWithKey:@"Team" ascending:NO];

    NSArray *sortDescriptors = [NSArray arrayWithObject:teams];


    [request setSortDescriptors:sortDescriptors];
    [teams release];

    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];


    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }

    // Save our fetched data to an array
    [self setStoredTeams:mutableFetchResults];
    NSLog(@"Number of teams, %i", [storedTeams count]);

//  NSMutableArray *temp = [[NSMutableArray alloc] init];

    for (Teams *diffTeams in storedTeams) {
        NSLog(@"Name: %@", diffTeams.Team);
        NSSet *rowers = [[NSSet alloc] initWithSet:diffTeams.Rowers];
        for (Rowers *roster in rowers){
            if ([diffTeams.Team isEqualToString:teamToFind]) {
                NSString *fullName = [NSString stringWithFormat:@"%@ %@", roster.FName, roster.LName];
                NSLog(@"%@", fullName);
                [teamRoster addObject:fullName];
                [fullName release];
                NSLog(@"Team Roster Array: %@", teamRoster);                
            }
        }
    }
    if (![context save:&error]) {
        //This is a serious error saying the record could not be saved.
        //Advise the user to restart the application
    }

    [mutableFetchResults release];
    [request release];
}
James Dunay
  • 2,714
  • 8
  • 41
  • 66
  • 2
    Are you retaining the object added to the `[appDelegate teamRoster]` correctly? , looks like the pointer was once set to an object that got released and the memory address reused for an other object. Please add the code where you set up the array. – rckoenes Jun 22 '11 at 15:06
  • well i will answer your question with a question, in my appDelegate, after setting the array, do i need to say [teamRoster retain]? before leaving? – James Dunay Jun 22 '11 at 15:09
  • depends on how you created the array in the first place. If you used a class method like [NSMutableArray array] then yes. If you used an alloc/init style creation then no (maybe) – Warren Burton Jun 22 '11 at 15:16
  • NSMutableArray *storedTeams; @property (nonatomic, retain) NSMutableArray *storedTeams; in the .h file – James Dunay Jun 22 '11 at 15:21
  • First comment was right on, i was releasing the NSString that i put into the object, but being in a for loop the object was created and released a second time. why does this have an effect on the array? – James Dunay Jun 22 '11 at 15:29
  • If this is a serious project familiarize yourself with the Big Ball of Mud concept http://www.codinghorror.com/blog/2007/11/the-big-ball-of-mud-and-other-architectural-disasters.html and why the app delegate while convenient should not be the place you are getting your data from http://stackoverflow.com/questions/5155437/is-it-good-practice-to-use-appdelegate-for-data-manipulation-and-handling Just a suggestion :) – Joe Jun 22 '11 at 15:36

2 Answers2

0

I think you want to be using NSStringFromClass([[[appDelegate teamRoster]objectAtIndex:1] class]) in your NSLog parameter.

bshirley
  • 8,217
  • 1
  • 37
  • 43
0

I was releasing the NSString that i put into the object, but being in a for loop the object was created and released a second time

James Dunay
  • 2,714
  • 8
  • 41
  • 66