HurkNburkS
HurkNburkS

Reputation: 5510

CoreData object not populating with values

I have this peice of code that reads my coreData and paces it into an array of coredata objects.. i then get one of these coredata objects into its own var type however not all of the attributes are populated to begin with.

this is what my code looks like.

NSMutableArray *tempFinishing = [coreDataController readFinishing];
    for (int i = 0; i < [tempFinishing count]; i++) {
        currentProject = [[Project alloc] init];
        currentProject = [tempFinishing objectAtIndex:i];

        if ([currentProject.hasChange isEqualToString:@"T"]) {

when i check hasChange it comes back as nil... but heres the weird thing if I do this in the console.

po currentProject.hasChange

returns nil

po currentProject.myID 

returns myID "1234"

then

po currentProject.hasChange

returns "F" // which is incorrect it should be set at T and have seen debugged it while it was being changed.... however this only works when i po it in the terminal..

UPDATE

now getting these warnings

Incompatible pointer types sending 'NSString *' to parameter of type 'NSEntityDescription *'
Incompatible pointer types initializing 'Project *' with an expression of type 'NSManagedObject *'

using this code.

NSManagedObjectContext *context = [self managedObjectContext];
        Project *currentProj = [[NSManagedObject alloc] initWithEntity:@"Project" insertIntoManagedObjectContext:context];

UPDATE 2

here is my coredata readFinishing method.

- (NSMutableArray *)readFinishing {
    NSManagedObjectContext *context = [self managedObjectContext];
    if (context == nil) {
        NSLog(@"Nil");
    }
    else {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSError *error;
        NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];
        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

        for (Project *projects in fetchedObjects) {
            [projectDictionaryArray addObject:projects];
        }
        return projectDictionaryArray;
    }
    return nil;
}

hopefully this helps shed some light.. I have been working on this for days.. :( lol

UPDATE 3

I think I may have found the problem.... in the core data object class I decided to randomly look at I found this..

enter image description here

updated to dynamic and testing now...

Upvotes: 1

Views: 551

Answers (3)

Javed Iqbal
Javed Iqbal

Reputation: 181

By looking into your code, I have found that "Project" is an entity of Core Data. So, In this piece of code, You should not create an entity like this, the way you are doing;

  for (int i = 0; i < [tempFinishing count]; i++) {
       currentProject = [[Project alloc] init];
       currentProject = [tempFinishing objectAtIndex:i];

       if ([currentProject.hasChange isEqualToString:@"T"]) {
       }
}

//Instead you should use the code like this:-

  for (int i = 0; i < [tempFinishing count]; i++) {

       //currentProject = [[Project alloc] init]; //Delete this line of code...
         currentProject = [tempFinishing objectAtIndex:i]; 

//And, you should read an attribute/property value of an entity like this

        [currentProject valueForKey:@"hasChange"];
//So, 

        if ([[currentProject valueForKey:@"hasChange"] isEqualToString:@"T"]) {
        }
}

Upvotes: 0

Bohdan Orlov
Bohdan Orlov

Reputation: 304

There is a few problems:

currentProject = [[Project alloc] init];
currentProject = [tempFinishing objectAtIndex:i];

1) you can't create Project as [[Project alloc] init]; but you have to use

Project *newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project"
inManagedObjectContext:context];

2) Second line currentProject = [tempFinishing objectAtIndex:i]; rewrites object "created" at line before it.

3) All your code (that was not generated by xCode) have to be in Project subclass. Because once you'll regenerate Projects class by xCode it will rewrite all your custom code. Thus consider to use mogenerator.

Upvotes: 0

Volker
Volker

Reputation: 4660

CoreData objects must be assigned to a context, and need to be created usually using NSEntitiyDescription

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

like:

Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:context];

More on creating of managed objects in code can be found for example here

Upvotes: 3

Related Questions