I have a number of View Controllers that need to show data from a Core-Data store.
Each of them fetch managed objects from the same context but for some reason the number of managed objects increases when more than once VC has made a fetch?
Can the managedObjects not be shared within the same context and only pointer reference?
Why is the number of managed objects increasing when the View Controllers request the same data?
Code:
- (void) updateCacheWithObject:(Object *)object
{
[self.coreDataSaveQueue addOperationWithBlock:^{
NSManagedObjectContext *saveContext = [[NSManagedObjectContext alloc] init];
[saveContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
AudioObject *object = [NSEntityDescription
insertNewObjectForEntityForName:@"object"
inManagedObjectContext:saveContext];
[audioObject setValue:object.localPath forKey:@"localPath"];
[audioObject setValue:object.title forKey:@"title"];
[audioObject setValue:object.data forKey:@"data"];
NSError *error;
// does the psc have a store
if ([saveContext.persistentStoreCoordinator.persistentStores count] == 0) {
[saveContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
}
if (![saveContext save:&error])
{
NSLog(@"Couldn't save: %@", [error localizedDescription]);
NSLog(@"Error user info dictionary is %@", [error userInfo]);
}
}