核心数据:executeFetchRequest vs performFetch [英] Core data: executeFetchRequest vs performFetch

查看:214
本文介绍了核心数据:executeFetchRequest vs performFetch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个关于两者之间比较的详尽列表。我知道的事情:






  • 发送到MOC的消息

  • 返回受管对象的数组

  • 目标: li>
  • 与表视图无关:与表视图无关

  • 频率:常用于循环,


performFetch




  • 发送到FRC的消息

  • 调用后,使用 fetchedObjects 返回受管对象数组

  • 使用表视图:FRC专门用于保持管理对象和表视图行同步,并使用 performFetch 初始化该进程。

  • 频率:通常只有一次。除非FRC的提取请求更改,否则不需要再次调用 performFetch



请纠正我,如果我错了,并附加列表。谢谢。

解决方案

关于 executeFetchRequest:


寄给MOC的讯息



返回受管对象的数组


也更改要检索的结果类型。在 NSFetchRequest 中,您可以设置不同的结果类型:

   - void)setResultType:(NSFetchRequestResultType)type 

其中 NSFetchRequestResultType 可以是不同类型。取自Apple doc:

 枚举{
NSManagedObjectResultType = 0x00,
NSManagedObjectIDResultType = 0x01,
NSDictionaryResultType = 0x02
NSCountResultType = 0x04
};
typedef NSUInteger NSFetchRequestResultType;




目标:从持久性存储中获取对象到MOC


是,创建一个 NSFetchRequest 并执行一个请求,与创建SELECT语句SQL。如果您还使用 NSPredicate ,它与使用SELECT-WHERE语句相同。


与表视图无关:与表视图无关


是,但可以使用检索数据填充表格<


频率:经常在循环中使用,因此可以多次调用


这取决于你想要达到的目的。它可以在一个循环内或不在。在循环内执行请求可能会影响性能,但我不会担心。 Core Data下维护着一种缓存机制。每次执行请求时,如果数据不在缓存中,Core Data会对您的商店执行往返(例如sql文件),并用其检索的对象填充缓存。如果执行相同的查询,由于缓存机制,往返将不会再次执行。无论如何,你可以避免在运行循环中执行一个请求,只需将该请求移出循环。



关于 performFetch:


发送到FRC的消息



调用后,使用fetchedObjects返回托管
对象的数组


是的,但您也可以使用 [_ fetchedResultsController objectAtIndexPath:indexPath]; 检索一个对象



在这里,我真的建议阅读一个很好的教程 NSFetchedResultsController


使用表视图:FRC专用于保持管理对象和
表视图行同步,并使用performFetch初始化
进程。


NSFetchedResultsController NSManagedObjectContext 结合使用。此外,它启用延迟加载数据。假设你有1000个元素检索,你想显示在 UITableView 。设置 NSFetchRequest 的请求如:

  [fetchRequest setFetchBatchSize:20 ]; 

并使用 NSFetchedResultsController ,它允许首先加载20个元素。然后滚动时,其他20个元素被加载,等等。如果没有 NSFetchedResultsController ,您必须手动实现此行为。请参阅我提供的更多信息。


频率:通常只有一次。除非FRC的fetch请求更改,否则
不需要第二次调用performFetch


这取决于您想要实现。



编辑

strong>



您必须明确调用 performFetch 。我喜欢在我的头文件(.h)中创建 NSFetchedResultsController 的属性,例如

  @property(nonatomic,strong,readonly)NSFetchedResultsController * fetchedResultsController; 

并将其合并到您的实施文件(.m),如

  @synthesize fetchedResultsController = _fetchedResultsController; 

然后总是在.m文件中覆盖getter来创建一个新的实例:

   - (NSFetchedResultsController *)fetchedResultsController 
{
//它已经存在,因此返回
if (_fetchedResultsController)return _fetchedResultsController;

// else创建它并返回

_fetchedResultsController = // alloc-init这里完成安装

return _fetchedResultsController;完成后,在您的类中(例如在中)
}


$ b < viewDidLoad
method)使用它像

  NSError * error = nil; 
if(![[self fetchedResultsController] performFetch:& error]){

//适当处理错误。
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
}


I want a thorough list regarding comparison between the two. Things I have known:

executeFetchRequest:

  • Message sent to MOC
  • Return an array of managed objects
  • Goal: fetch objects from persistent store to MOC
  • With table view: has nothing to do with table view
  • Frequency: often used in a loop, so could be called many many times

performFetch:

  • Message sent to FRC
  • After calling it, use fetchedObjects to return an array of managed objects
  • With table view: FRC is specifically for keeping managed objects and table view rows in sync, and use performFetch to initialize that process.
  • Frequency: often only once. Unless fetch request of FRC changes, no need to call performFetch a second time

Please correct me if I am wrong and append the list. Thank you.

解决方案

About executeFetchRequest:

Message sent to MOC

Yes

Return an array of managed objects

Yes, but you can also change the type of results you want to retrieve. In NSFetchRequest you can set a different result type with:

- (void)setResultType:(NSFetchRequestResultType)type

where NSFetchRequestResultType can be of different types. Taken from Apple doc:

enum {
   NSManagedObjectResultType        = 0x00,
   NSManagedObjectIDResultType      = 0x01,
   NSDictionaryResultType           = 0x02
   NSCountResultType                = 0x04
};
typedef NSUInteger NSFetchRequestResultType; 

Goal: fetch objects from persistent store to MOC

Yes, creating a NSFetchRequest and performing a request, it the same as creating a SELECT statement in SQL. If you also use a NSPredicate it's the same as using SELECT-WHERE statement.

With table view: has nothing to do with table view

Yes, but with retrieved data you can populate a table

Frequency: often used in a loop, so could be called many many times

It depends, on what you want to achieve. It could be within a loop or not. Executing the request within a loop could have impact on performance but I would not be worried on that. Under the hood Core Data maintains a sort of cache mechanism. Every time you perform a request, if data are not in the cache, Core Data executes a round trip on your store (e.g. sql file) and populate the cache with the objects it has retrieved. If you perform the same query, the round trip will not performed again due to the cache mechanism. Anyway, you could avoid to execute a request within the run loop, simply moving that request outside the loop.

About performFetch:

Message sent to FRC

Yes

After calling it, use fetchedObjects to return an array of managed objects

Yes, but you can also retrieve an object with [_fetchedResultsController objectAtIndexPath:indexPath]; if you are populating a specific cell within a table.

Here I really suggest to read a nice tutorial on NSFetchedResultsController

With table view: FRC is specifically for keeping managed objects and table view rows in sync, and use performFetch to initialize that process.

Yes, a NSFetchedResultsController works in combination with a NSManagedObjectContext for you. Furthermore, it enables lazy loading of data. Suppose you have 1000 elements you retrieve and you want to display them in a UITableView. Setting a request for a NSFetchRequest like:

[fetchRequest setFetchBatchSize:20];

and using it with an instance of a NSFetchedResultsController, it allows to load 20 elements at first. Then when you scroll, other 20 elements are loaded, and so on. Without a NSFetchedResultsController you must implement this behavior manually. Refer to the tutorial I provided for further info.

Frequency: often only once. Unless fetch request of FRC changes, no need to call performFetch a second time

It depends on what you want to achieve. Most of the time you could call it once.

Hope that helps.

Edit

You have to call performFetch explicitly. I like to create a property for NSFetchedResultsController in my header file (.h) like

@property (nonatomic, strong, readonly) NSFetchedResultsController* fetchedResultsController;

and synthesize it in your implementation file (.m) like

@synthesize fetchedResultsController = _fetchedResultsController;

Then always within the .m file override the getter to create an new instance of it:

- (NSFetchedResultsController*)fetchedResultsController
{
    // it already exists, so return it
    if(_fetchedResultsController) return _fetchedResultsController;

    // else create it and return

    _fetchedResultsController = // alloc-init here with complete setup

   return _fetchedResultsController;
}

Once done, within your class (for example in viewDidLoad method) use it like

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {

    // Handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

这篇关于核心数据:executeFetchRequest vs performFetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆