使用 FetchResultController 和 ManagedObjectContext 获取对象之间的区别

发布于 2024-08-27 07:55:33 字数 1142 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-09-03 07:55:33

使用 NSFetchedResultsController 而不是仅使用 NSFetchRequest 的目的是监视数据以及使用节时的便捷方法。

当仅处理 NSFetchRequest 时,您必须自己确定各个部分,并且需要在发生变化时重新获取数据。

在处理 NSFetchedResultsController 时,它将确定您的部分,缓存结果(几乎瞬时对该数据发出第二次请求),并为您的 NSTableView 提供便捷的方法。最后,当您的数据发生更改时,NSFetchedResultsController 将通过其委托通知您。

这两者的内部数据将是相同的。区别在于管理数据的状态。

The point behind using an NSFetchedResultsController as opposed to just a NSFetchRequest 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, the NSFetchedResultsController 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.

梦魇绽荼蘼 2024-09-03 07:55:33

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 for NSFetchedResultsControllerDelegate protocol).
It also provide you with section management which is useful if you want to display the data in a table view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文