soleil
soleil

Reputation: 13105

Managing core data objects with a data manager singleton

I have a set of items in a plist. When my app starts, I read in the plist and save it as an array in my DataManager singleton, like this:

NSString *path = [[NSBundle mainBundle] bundlePath];

NSString *itemDatapath = [path stringByAppendingPathComponent:@"ItemData.plist"];
NSDictionary *itemData = [NSDictionary dictionaryWithContentsOfFile:itemDatapath];
dataManager.items = [itemData objectForKey:@"Items"];

I also want to store the core data objects that are associated with this data in the DataManger, so I attempted this:

-(void)setItems:(NSArray *)_items //causes EXC_BAD_ACCESS error
{
self.items = _items;

NSManagedObjectContext *context = [self managedObjectContext];


for (NSDictionary *item in self.items)
{
    NSManagedObject *itemObject = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"Item"
                                   inManagedObjectContext:context];
    [itemObject setValue:[NSNumber numberWithInteger:[[item valueForKey:@"id"] intValue]] forKey:@"identifier"];
    [itemObject setValue:[UIImage imageNamed:[item valueForKey:@"image"]] forKey:@"image"];
    ...

}


NSError *error;
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}

The point being that anywhere in my app I can access the objects from this method:

-(NSArray*)fetchItems
{
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Item" inManagedObjectContext:managedObjectContext];



NSError *error2;
NSFetchRequest *itemFetchRequest = [[NSFetchRequest alloc] init];
[itemFetchRequest setEntity:entity];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order"
                                                               ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[itemFetchRequest setSortDescriptors:sortDescriptors];

NSArray *fetchedItems = [managedObjectContext executeFetchRequest:itemFetchRequest error:&error2];


return fetchedItems;


}

The problem is the EXC_BAD_ACCESS error noted above. I would also like to know if there is a better way of going about this. I have the feeling storing the core data objects here is not the best practice. But even if I fetch the data when I need it in other view controllers, how can I manage updating the core data objects if they change? I have an external plist that may change, and the core data objects need to update based on that.

Upvotes: 0

Views: 595

Answers (1)

Carl Veazey
Carl Veazey

Reputation: 18363

You are causing infinite recursion when you put self.items = _items inside the setItems: method. self.items is exactly the same as calling setItems - they invoke the same method. What you need to do instead is set the value of whatever your instance variable is - presumably items. So the first line of setItems: should be items = _items. That, in and of itself, is also confusing, as the convention is to have _ before variables indicate an instance variable.

Upvotes: 2

Related Questions