核心数据:[NSFetchedResultsController -fetchedObjects],这个方法会反映对象的变化吗? [英] core data: [NSFetchedResultsController -fetchedObjects], will this method reflect object change?

查看:268
本文介绍了核心数据:[NSFetchedResultsController -fetchedObjects],这个方法会反映对象的变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用一个 NSFetchRequest 创建一个 NSFetchedResultsController ,获取实体Entity的所有对象。



,然后我插入一个新的NSManagedObject A,它是一个Entity的实例,而Edit B也是一个Entity的实例。



那么我调用 NSArray * result = [fetchedResultsController -fetchedObjects] ,结果中会有A和B吗?



在整个项目中,我使用只有一个 NSManagedObjectContext。



如果我这样做, a href =http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html =nofollow>文档?


fetchedObjects获取的结果。



@property(nonatomic,readonly)NSArray * fetchedObjects讨论



结果数组只包括
指定的实体的实例抓取请求(fetchRequest),并匹配其谓词。 (如果
fetch请求没有谓词,则results数组包括获取请求指定的实体的所有
实例。)



results数组反映了
中的受管对象的内存中状态,即控制器的受管对象上下文,而不是它们在
持久存储中的状态。但是,返回的数组不会更新为
管理对象被插入,修改或删除。



解决方案

最后一句的含义


...但是,返回的数组不会更新为托管对象


是(根据我的实验):如果您存储对获取的对象

  NSArray * result = [fetchedResultsController fetchedObjects]; 
code>将不会更新,如果对象被插入,修改或删除。
所以重点是 返回的数组 不...。



但是如果调用

  [fetchedResultsController fetchedObjects] 

稍后,返回值是一个新的对象列表,包括更改。



em>如果已设置委托,则FRC跟踪对托管对象上下文的更改并调用FRC委派函数。当对象被插入/修改/删除时,不是立即调用委托函数,而是当上下文显式保存时







controllerDidChangeContent:被调用,对 [fetchedResultsController fetchedObjects] 的新调用返回更新的对象列表。


first I create a NSFetchedResultsController with a NSFetchRequest that fetches all object of entity "Entity".

and then I insert an new NSManagedObject A ,which is an instance of "Entity", and Edit B, which is also an instance of "Entity".

then I call NSArray *result = [fetchedResultsController -fetchedObjects], will A and B in result?

Throughout the project, I use only one NSManagedObjectContext.

if I do, what does this mean in its Document?

fetchedObjects The results of the fetch.

@property (nonatomic, readonly) NSArray *fetchedObjects Discussion The value of the property is nil if performFetch: hasn’t been called.

The results array only includes instances of the entity specified by the fetch request (fetchRequest) and that match its predicate. (If the fetch request has no predicate, then the results array includes all instances of the entity specified by the fetch request.)

The results array reflects the in-memory state of managed objects in the controller’s managed object context, not their state in the persistent store. The returned array does not, however, update as managed objects are inserted, modified, or deleted.

解决方案

The meaning of the last sentence

... The returned array does not, however, update as managed objects are inserted, modified, or deleted.

is (based on my experiments): If you store a reference to the fetched objects

NSArray *result = [fetchedResultsController fetchedObjects];

then this array referenced by result will not update if objects are inserted, modified or deleted. So the emphasis is on "the returned array does not ...".

But if you call

[fetchedResultsController fetchedObjects]

later again, the return value is a new list of objects, including the changes.

More precisely: If you have set a delegate, the FRC tracks changes to the managed object context and calls the FRC delegate functions. The delegate functions are not called immediately when objects are inserted/modified/deleted, but either

  • when the context is saved explicitly, or
  • when the change notifications are processed in the run loop.

As soon as controllerDidChangeContent: is called, a new call to [fetchedResultsController fetchedObjects] returns the updated object list.

这篇关于核心数据:[NSFetchedResultsController -fetchedObjects],这个方法会反映对象的变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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