Craig
Craig

Reputation: 556

Working with retrieved core data

Can't seem to find any Q&A or anything that helps me with this, but if you find some feel free to link! I've extracted data from a Core Data entity and using setPropertiesToFetch i have extracted a list of 'line numbers'. Now what i want is to take any line numbers that are set as nil and change them to 0.

Problem 1) 'unrecognized selector sent to instance 0x59ef30'. I know this error is from the 'if' line through commenting out others but i'm not sure what has caused it!

Problem 2) What i want it to do after increasing count is run again so it checks the next object but i'm really unsure as to the best code to do this with. Any suggestions of methods/code snippets/tutorials will be lovely!

Here is the code (The fetch request above works just fine :) )

changeTo0 = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];
NSLog(@"What is even in this array anyway? %@", changeTo0);
[fetchRequest release];

NSLog(@"In the array there is %u records", [changeTo0 count]);

NSInteger count = 0;
Fsode *details = [changeTo0 objectAtIndex:count];
NSLog(@"In details we have %@", details);        

if ([details lineNumber] == nil){
   [details setValue:0 forKey:@"lineNumber"];
   ++count
   NSLog(@"HERE");
}

Upvotes: 0

Views: 905

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

About the first question, you should say something about the error (see Phillip Mills comment) and model. Does Fsode entity have a property called lineNumber? What type is it?

About the second question, execute only once the request like the following:

NSArray* results = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"What is even in this array anyway? %@", results);

int count = 1;
for(NSManagedObject* obj in results) {

    // I suppose lineNumber is of type NSNumber
    int numberValue = [[obj valueForKey:@"lineNumber"] intValue];    
    if(numberValue == 0) {

        [obj setValue:[NSNumber numberWithInt:count] forKey:@"lineNumber"];
        count++;
    }
}    

[fetchRequest release];

// do a save here...

P.S. Reading your code, I'm not sure about the second question. Feel free to explain what you want to achieve.

The code I used increment the lineNumber for each object that has a line number of zero. So the first object has 1, the second 2 and so on. If you want to put a constant line number for those objects replace

[obj setValue:[NSNumber numberWithInt:count] forKey:@"lineNumber"];

with

[obj setValue:[NSNumber numberWithInt:1] forKey:@"lineNumber"]; and remove the count (it's the same as your code does).

Upvotes: 4

Related Questions