Reputation: 324
I'm having a issue with something I'm working one. My application so far consists of two UITableView controllers and a single ViewController. The app simply asks the user to enter the name of a list on UIAlert and it saves to core data, and the name of the list is put into the first tableview. Then the user clicks on the name of the list and it pushes them to the contents on the list, which is empty because it hasn't been populated yet. Then the user populates the TableView, so it pushes to the single view controller where you would enter all the info and hit save and it saves as an item. So my problem is when I go to add an item it saves but then when I restart my app it the list is still there but all the items in the list are gone. I have have no idea why it's doing this do if you have any suggestions let me know! Thanks!
I put the project on GitHub because there is a lot of code, also it allows you to experience my issue, and understand the flow of the controllers. https://github.com/jackintosh7/test-master
My Add Item .m:
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
[newItem setValue:self.name.text forKey:@"itemName"];
[newItem setValue:self.price.text forKey:@"price"];
[newItem setValue:self.desc.text forKey:@"description"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
Upvotes: 0
Views: 494
Reputation: 51
Very helpful for the future: As a sanity check I'd first check to see if the items in your list are actually being saved. You can use the simulator and a tool like CoreDataPro to view your store. When I was first learning iOS programming and Core Data this was extremely helpful: https://github.com/yepher/CoreDataUtility
Using the above utility, I saw your items were in fact not saving to your persistent store.
I took a look at your revised code and made a few small edits and all seems to be working.
1) In AddViewController.h : Import item.h and list.h. Most importantly, add a List strong property to your AddViewController so your new Item has a reference to the applicable list.
#import "Item.h" //Your forward @class definitions would probably work.
#import "List.h"
@property (nonatomic, strong) List *list;
2) Reduced code in WishListView.m prepareForSegue: Simply passing the List and ManagedObjectContext to the destination ViewController.
else if( [[segue identifier] isEqualToString:@"AddItem"] ) {
destViewController.list = self.list;
destViewController.managedObjectContext = self.managedObjectContext;
destViewController.completionBlock = ^(BOOL saved){
_items = nil;
[[self tableView] reloadData];
};
}
3) Creating the NSEntityDescription inside AddViewController.m instead of passing it. Also saving the List from the new list
property we created.
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
self.item = [NSEntityDescription insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:context];
self.item.name = self.name.text;
self.item.price = self.price.text;
self.item.desc = self.desc.text;
self.item.list = self.list;
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
if( self.completionBlock ) {
self.completionBlock(YES);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Upvotes: 2