Datos básicos, NSMutableArray contiene NSManagedObject, los objetos se vuelven nulos después de insertar el primer NSManagedObject de la matriz

I am inserting an array of NSManagedObject using coreData, after i successfully insert my first object, the second object of the NSMutableArray becomes nil Why is that? here is a snapshot of my code:

+ (void)insertCategoryUsingCategoryArray:(NSMutableArray *)categoryArray
    for (int i=0;i<[categoryArray count]; i++) {
        DataCategory *temp=(DataCategory *)[categoryArray objectAtIndex:i];

        DataCategory *categoryInfo=(DataCategory *)[NSEntityDescription insertNewObjectForEntityForName:@"DataCategory" inManagedObjectContext:_sing.managedObjectContext];

        [categoryInfo setCategoryId:temp.categoryId];
        [categoryInfo setName:temp.name];
        [categoryInfo setImgSrc:temp.imgSrc];

        NSError *error;
        if (![_sing.managedObjectContext save:&error]) {
            NSLog(@"An error occured while saving! shit : %@", [error localizedDescription]);

When i enter the insert function i debug the categoryArray, all objects are there, after looping the first time temp becomes nil for some reason, anyone knows why?

preguntado el 03 de mayo de 12 a las 10:05

An instance of an NSArray should return nil only if the array itself is nil, this is because sending a message to nil return nil. Check if categoryArray still exist after the save message. -

categoryArray still exists but all the DataCategory inside categoryArray are now nil after the first loop in the for -

1 Respuestas

categoryArray should not be an argument to this method. In your setup, the method gets passed an array as a variable which is private to the method. It is unpredictable what this array will contain once you leave the method unless you return something.

Rather, you should have an ivar managed by the view controller. Initialize the ivar with

categoryArray = [NSMutableArray array];

in viewDidLoad and then use this variable and do not pass an array to the method.


PS: You could improve your programming style by eliminating invectives.;-)

contestado el 03 de mayo de 12 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.