0

So I have this NSArray with NSDictionaries inside.

As you can see in the code there are actually two very equal for loops (don't mind why).

If you see the log, the key scheduleID in the first object in the second loop is suddenly nil...

I have no clue whatsoever how this is even possible. The object (NSDictionary) itself is not nil. I am using ARC.

UILocalNotification *mergedNotification = [notificationsWithEqualFireDates objectAtIndex:0];

    if ([notificationsWithEqualFireDates count] > 1) {
        NSMutableArray *mergedUserInfo = [[NSMutableArray alloc] init];
        for (UILocalNotification *ln in notificationsWithEqualFireDates) {
            NSLog(@"Hmmm... %@", [ln.userInfo objectForKey:@"scheduleID"]);
            [mergedUserInfo addObject:ln.userInfo];
        }
        NSDictionary *mergedUserInfoDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:mergedUserInfo,@"array", nil];
        mergedNotification.userInfo = mergedUserInfoDictionary;
    }

    for (UILocalNotification *tempNotification in notificationsWithEqualFireDates) {
        NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"RegisteredNotifications" inManagedObjectContext:newMoc];
        [managedObject setValue:tempNotification.fireDate forKey:@"fireDate"];
        [managedObject setValue:[tempNotification.userInfo objectForKey:@"dateTimeOriginal"] forKey:@"dateTimeOriginal"];
        [managedObject setValue:[tempNotification.userInfo objectForKey:@"scheduleID"] forKey:@"scheduleID"];
        NSLog(@"Saving schedule: %@", [tempNotification.userInfo objectForKey:@"scheduleID"]);
    }

enter image description here

UPDATE I was able to fix this issue by using the copy method:

UILocalNotification *mergedNotification = [[notificationsWithEqualFireDates objectAtIndex:0] copy];

Can somebody please explain why this was happening and why I need this copy method. I would think that assigning an object from an NSArray to a variable would be to create a memory location, init it and then copy the value from the place in the array to the variable...

1
  • Why do all the objects have the same Core Data object ID? Commented Apr 19, 2012 at 22:16

3 Answers 3

0

The only thing I can think of is that notificationsWithEqualFireDates is going out of scope somehow, it is an NSDictionary, correct? Is it a property? I encountered a case where a NSString went to nil that was set as a weak property in another class inside of a method, maybe something similar? Suggest you look into that and/or post more code about the origin of notificationsWithEqualFireDates. Another to try would be to set breakpoints and watch the stack as you go through each line above to see when it goes to nil...

Sign up to request clarification or add additional context in comments.

Comments

0

notificationsWithEqualFireDates is being released. Try using a property w/ strong reference.

Comments

0

Guess:

Print tempNotification.userInfo.

It is probably the same as whatever you shoved into mergedUserInfoDictionary; i.e. you are overwriting the userInfo entirely in that code.

Can't say conclusively because you don't show where mergedNotification comes from.


Could you please explain how this is happening?

You are replacing the userInfo of the NSNotification instance in slot 0 of your array with a dictionary that no longer has the key you are looking up.

2 Comments

Right above the if I do this: UILocalNotification *mergedNotification = [notificationsWithEqualFireDates objectAtIndex:0];
Could you please explain how this is happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.