xcode 使用 NSFetchRequest (CoreData) 填充数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8192902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill an array using NSFetchRequest (CoreData)
提问by DAS
I created a project for iPad which uses Apple's "Use CoreData"
template.
我为 iPad 创建了一个使用 Apple"Use CoreData"
模板的项目。
I customized several things, e.g. the names of the objects, but the system stayed the same.
我自定义了一些东西,例如对象的名称,但系统保持不变。
Apple uses a MasterViewController.h
which is a UITableViewController
und a DetailViewController.h
which is a simple UIViewController
. Once you have selected one of the objects in the UITableView
in MasterViewController.h
it gives its attributes to the DetailViewController
.
苹果采用的是MasterViewController.h
这是一个UITableViewController
UND一个DetailViewController.h
是一个简单的UIViewController
。一旦您选择了其中的一个对象UITableView
,MasterViewController.h
它就会将其属性赋予DetailViewController
.
Now the problem:
现在的问题:
I created a third view controller, named ResultsViewController.h
which should display all the data (attributes of the objects).
我创建了第三个视图控制器,命名为ResultsViewController.h
它应该显示所有数据(对象的属性)。
I want to use one or more NSArray(s)
, which should contain all my data (not just the selected but all. So if you created 5 objects, you want to have 5 times NSArray
).
我想使用一个或多个NSArray(s)
,它应该包含我的所有数据(不仅仅是选定的,而是所有的。所以如果你创建了 5 个对象,你想要有 5 次NSArray
)。
(1) How can I fetch the values of the xcoredatamodel
to be able to access them in ResultsViewController
? Sending a fetch request did not work for me?
(1) 如何获取 的值xcoredatamodel
以便能够访问它们ResultsViewController
?发送获取请求对我不起作用?
(2) How can I determine how many objects I created in my UITableView
(MasterViewController.h
)?
(2) 如何确定我在UITableView
( MasterViewController.h
) 中创建了多少个对象?
CoreData Structure:
核心数据结构:
Simple, one entity, several attributes, that is it!
简单,一个实体,几个属性,就是这样!
Thanks a lot!
非常感谢!
回答by Mundi
First: you should not put all your data into arrays. This will result in memory problems once your data volume grows. Core data is actually designed to fetch the data you need very efficiently without the need of maintaining expensive parallel data structures.
第一:您不应该将所有数据放入数组中。一旦您的数据量增长,这将导致内存问题。核心数据实际上旨在非常有效地获取您需要的数据,而无需维护昂贵的并行数据结构。
So the preferred way is to pass the same NSManagedObjectContext
to the ResultsViewController
and have it fetch its own data as needed.
所以首选的方法是将相同的内容传递NSManagedObjectContext
给ResultsViewController
并让它根据需要获取自己的数据。
However, if you really want to fetch all the data, it is simple.
但是,如果您真的想获取所有数据,这很简单。
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *fetchedObjects = [self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
The array fetchedObjects
now contains all of your data. You can access the attributes in the usual way (e.g. with dot notation or KVC).
该数组fetchedObjects
现在包含您的所有数据。您可以以通常的方式访问属性(例如,使用点符号或 KVC)。