0

I'm quite new with Core Data, and I'm having an issue that I don't understand. I don't know what's going wrong.

I'm saving into my Persistent Store, 7 objects of an entity "Weight" that is read from a JSON file with this code:

for (NSDictionary *values in aWeightValues) { weightValues = [NSEntityDescription insertNewObjectForEntityForName:@"Weight" inManagedObjectContext:moc]; [weightValues setValue:[typeWeight objectForKey:@"unit"] forKey:@"unit"]; [weightValues setValue:[values objectForKey:@"timestamp"] forKey:@"date"]; [weightValues setValue:[values objectForKey:@"value"] forKey:@"amount"]; if (![moc save:&error]) { NSLog(@"Problem saving: %@", [error localizedDescription]); } } 

The for loop makes 7 loops, that means it's being saved correctly (speaking about the number of objects).

But then, when I try to retrieve data from the Persistent Store in this way:

-(NSMutableArray *) extractWeightEntities { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; NSError *error; NSManagedObjectContext *moc = [appDelegate managedObjectContext]; NSEntityDescription *entityWeight = [NSEntityDescription entityForName:@"Weight" inManagedObjectContext:moc]; NSFetchRequest *request = [[[NSFetchRequest alloc]init]autorelease]; [request setEntity:entityWeight]; entityWeight = nil; fetchResult = [[moc executeFetchRequest:request error:&error]mutableCopy]; return (fetchResult); 

}

and try to show one attribute of each object retrieved, I get 1044 rows in my TableView!! when I should have just 7.

What am I doing wrong? Is the problem when I'm saving, or when I'm retrieving?

I hope you can help to solve this issue. Many thanks in advance!!

1 Answer 1

1

You don't need to call save on each iteration of the loop, this is very inefficient. Save afterwards.

Put a breakpoint on you loop and ensure it is only going over it 7 times.

Is the data continuously accumulating? Are you deleting the app each time? If you keep running the code - it will keep adding objects to your datastore unless you check if they exist in the datastore before inserting them.

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

5 Comments

But if don't do save in each iteration, am i overriding the attribute every time? Will it save the 7 different attributes? Ok, I'm not deleting the app each time, and I'm not deleting data in Persistent store neither. Thanks for that. How can I check so I don't duplicate rows in the database?
insertNewObjectForEntityForName will create a new object in the managed object context every time its called. The managed object context is the 'in memory' part of core data. When you call save, it saves the state of your managed object context to the persistent backing store. I notice you're then storing that in an instance variable - do you need to do that? - so yes, you're overriding the weightValues each time - however, every iteration is a new weightValues object that is stored in your MOC.
You check for duplicates by using the 'find-and-create' pattern - you do an NSFetchRequest and see if core data already has what you are inserting (using an NSPredicate as a 'where claus' to filter your results. You then only insert new object if the fetch fails. You need to be efficient with this though, as continually fetching will hurt performance. See core data programming guide "Implementing Find-or-Create Efficiently"
Thank you very much :) If it's not an instance variable how do I store it?
"Always give object the most restrictive scope possible first". This is a rule in programming. So your most restrictive scope here is inside your loop. So WeightValues *weightValue = [NSEntityDescription insertNewObjectForEnti.... etc inside your loop.... that is of course unless you need the object after your loop has finished. Is this what you wanted to know...or what exactly do you mean about "how do i store it"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.