使用 FetchResultController 和 ManagedObjectContext 获取对象之间的区别
使用 FetchResultController 或 ManagedObjectContext 从 Core Data 获取元素有什么区别?
1) FetchResultController
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext: managedObjectContext]];
NSSortDescriptor *sortDescriptorNameAscending = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorNameAscending,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptorNameAscending release];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Lists"];
2) ManagedObjectContext
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext]];
NSError *error = nil;
NSArray *items = [ managedObjectContext executeFetchRequest:request error:&error];
What's the difference between get elements from Core Data with FetchResultController or ManagedObjectContext??
1) FetchResultController
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext: managedObjectContext]];
NSSortDescriptor *sortDescriptorNameAscending = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorNameAscending,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptorNameAscending release];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Lists"];
2) ManagedObjectContext
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext]];
NSError *error = nil;
NSArray *items = [ managedObjectContext executeFetchRequest:request error:&error];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
NSFetchedResultsController
而不是仅使用NSFetchRequest
的目的是监视数据以及使用节时的便捷方法。当仅处理
NSFetchRequest
时,您必须自己确定各个部分,并且需要在发生变化时重新获取数据。在处理 NSFetchedResultsController 时,它将确定您的部分,缓存结果(几乎瞬时对该数据发出第二次请求),并为您的 NSTableView 提供便捷的方法。最后,当您的数据发生更改时,
NSFetchedResultsController
将通过其委托通知您。这两者的内部数据将是相同的。区别在于管理数据的状态。
The point behind using an
NSFetchedResultsController
as opposed to just aNSFetchRequest
is the monitoring of your data and the convenience methods when working with sections.When dealing with just a
NSFetchRequest
you have to determine the sections yourself and you need to refetch your data when something changes.When dealing with the
NSFetchedResultsController
, it will determine your sections, cache the results (making a second request for that data near instantaneous), and provide convenience methods for your NSTableView. Finally, when your data changes, theNSFetchedResultsController
will notify you through its delegates.The data internal to both of these is going to be the same. It is managing the state of that data that is the difference.
NSFetchedResultsController
对象会告诉您查询对象何时发生变化。只需提供一个委托对象来接收调用(请参阅 NSFetchedResultsControllerDelegate 协议的文档)。它还为您提供部分管理,如果您想在表视图中显示数据,这非常有用。
The
NSFetchedResultsController
object will tell you when the objects of your query change. Just provide a delegate object to receive the calls (see the doc forNSFetchedResultsControllerDelegate
protocol).It also provide you with section management which is useful if you want to display the data in a table view.