从多个表视图保存核心数据
我有一个具有多个表视图的应用程序,我想使用 Core Data 来捕获所有数据。我有两个实体 - 冰柜和物品。在第一个表格视图中,我添加了一个冰箱,它可以正确保存。我退出应用程序,重新打开,它就在那里。我单击冰箱(打开另一个表格视图)并添加一些项目,我可以在新的分区表格视图中看到它们。我退出我的应用程序,重新启动它,查看冰箱,单击它,但没有任何物品。
我的 appDelegate 中有 ManagedObjectContext ,并使用所有视图从那里引用它,所以我没有创建多个实例。以下是我用来将项目保存到冷冻柜的代码,包括 ManagedObjectContext 和我的 itemsArray:
Item *item = (Item *)[NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[delegate managedObjectContext]];
[item setFreezer:freezerName];
[item setName:name];
[item setQuantity:quantity];
[item setSection:section];
[item setAdded:added];
[item setNotes:notes];
NSError *error = nil;
if (![[delegate managedObjectContext] save:&error]) {
NSLog(@"Freezer info didn't save. Need to handle this.");
}
[items insertObject:item atIndex:0];
这是我在 ItemViewController 中用于检索 viewDidLoad 中的项目的代码:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"freezer == '%@'", freezerName];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[delegate managedObjectContext]];
NSSortDescriptor *sorts = [[NSSortDescriptor alloc] initWithKey:@"section" ascending:NO];
NSArray *sort = [[NSArray alloc] initWithObjects:sorts, nil];
[request setSortDescriptors:sort];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error = nil;
NSMutableArray *results = [[[delegate managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
if(results == nil) {
NSLog(@"Error fetching results... need to handle");
}
[self setItems:results];
NSLog(@"items count:%d", [items count]);
返回的项目计数为零。
我完全被难住了,花了几个小时在网上搜索,尝试不同的东西,但我无法弄清楚。我知道有一些更聪明的程序员,我希望你们中的一个人能够看到问题所在。
I have an app that has multiple tableviews and I want to use Core Data to capture all the data. I have two entities - freezers and items. In the first tableview I add a freezer and it saves correctly. I quit the app, re-open, and it is there. I click on the freezer (opening another tableview) and add some items and I can see them in my new sectioned tableview. I quit my app, restart it, see the freezer, click on it and there are no items.
I have my managedObjectContext in my appDelegate and reference it from there using all views, so I am not creating multiple instances. Here is the code I use to save the items to a freezer, both the managedObjectContext and my itemsArray:
Item *item = (Item *)[NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[delegate managedObjectContext]];
[item setFreezer:freezerName];
[item setName:name];
[item setQuantity:quantity];
[item setSection:section];
[item setAdded:added];
[item setNotes:notes];
NSError *error = nil;
if (![[delegate managedObjectContext] save:&error]) {
NSLog(@"Freezer info didn't save. Need to handle this.");
}
[items insertObject:item atIndex:0];
Here is the code I use in the ItemViewController to retrieve the items within viewDidLoad:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"freezer == '%@'", freezerName];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[delegate managedObjectContext]];
NSSortDescriptor *sorts = [[NSSortDescriptor alloc] initWithKey:@"section" ascending:NO];
NSArray *sort = [[NSArray alloc] initWithObjects:sorts, nil];
[request setSortDescriptors:sort];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error = nil;
NSMutableArray *results = [[[delegate managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
if(results == nil) {
NSLog(@"Error fetching results... need to handle");
}
[self setItems:results];
NSLog(@"items count:%d", [items count]);
The item count returned is zero.
I am completely stumped and have spent several hours searching online, trying different things, and I can't figure it out. I know there are some much smarter coders out there and I hope one of you can see what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试将谓词更改为
希望有帮助!
I would try to change the predicate to
Hope that helps!
也许问题是您在
方法中加载数据。它仅在加载视图时调用一次,因此当底层数据发生更改时,您的视图控制器不知道它。
您可以将加载代码移至
方法或引入通知来传播数据存储已更改其状态的信息,并根据该事件重新加载表视图。
最好的方法可能是使用 NSFetchedResultsController 作为数据源,因为它始终了解其数据存储更改。检查文档以获取该类的参考。
Maybe the problem is that you load data in
method. It's called only once when your view is loaded, so when underlaying data get's changed, your view controller is not aware about it.
You can either move your loading code to
method or introduce notifications to spread the information that data store has changed its state and reload table views upon that event.
The best way is probably to use NSFetchedResultsController as your data source, as its always aware of its data store changes. Check docs for reference to that class.