NSFetchResultsController w/ NSPredicate(关系)与基本 NSFetchRequest(包含图片)

发布于 2024-10-21 21:59:23 字数 2596 浏览 1 评论 0原文

这是我正在辩论的关系的图像: Core Data Setup

我有一个 UIScrollView 设置作为水平滚动条,在 3 个不同的 UIViewController 之间滚动(当然包含 UITableView 和

UIScrollView 中的每个 ViewController 都会加载特定 MyObjectType 的 UITableView。 (例如 ViewController1 加载所有 MyObjects 的表格视图,其中 type == MyObjectType.name

这有意义吗?您会注意到我在对象之间设置了反向关系。一个 MyObjectType 可以有多个 MyObject,但一个 MyObject 只能有一个与其关联的 MyObjectType

当我第一次加载 UIScrollView viewController 之一时,我需要确定此 UITableView 的 MyObjectType 用途。我的工作正常,并相应地设置了表头。

例如 [type valueForKey:@"name"],其中 typeMyObjectType 的 NSManagedObject 获取结果。

问题是我想知道,当我获得 MyObjectType 的 NSManagedObject 时,我是否也无法访问 NSSet *array(即 [type valueForKey:@"objects"]) 我可以使用它作为 UITableView 的数据源吗?如果我添加删除对象后我保存托管上下文,然后我总是[tableView reloadData],这会起作用吗?

我猜这会起作用,只要我不要求 UITableView 内容在添加这种类型的新 MyObject 时动态更改和更新?为此我们需要一个 NSFetchedResultsController 对吗?

MyObject 加载到 UITableView 中的代码(可以工作)

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"MyObject" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                          initWithKey:@"creationDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                    managedObjectContext:managedObjectContext sectionNameKeyPath:@"transientSectionDate" 
                                               cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return _fetchedResultsController;    

这是我将 ALL MyObject 的 MyObjectType.name == @"XXXXXX"?假设我已经有一个 MyObjectType.name 存储在 ViewController 内保留的 NSString 中。

提前致谢!

Here is an image of the relationship I am debating:
Core Data Setup

I have a UIScrollView setup as a horizontal scroller that scrolls between 3 different UIViewControllers (containing a UITableView of course and the required delegate methods, etc.)

Each ViewController in the UIScrollView loads a UITableView of a specific MyObjectType.
(E.g. ViewController1 loads a tableview of all MyObjects where its type == MyObjectType.name)

Does this make sense? You'll notice I've setup an inverse relationship between the objects. A MyObjectType can have many MyObject's but a MyObject can only have a single MyObjectType associated to it.

When I first load one of the UIScrollView viewController's I need to determine what MyObjectType this UITableView is for. I have this working fine and I set the Table Header accordingly.

E.g. [type valueForKey:@"name"] where type is a fetched result NSManagedObject of MyObjectType.

The thing is I'm wondering, when I obtain this NSManagedObject of MyObjectType do I not also have access to a NSSet *array (ie. [type valueForKey:@"objects"]) which I can use as the UITableView's datasource? Would this work if after I add or delete an object I save the managedContext and then I always [tableView reloadData] ?

I'm guessing this would work, as long as I don't require the UITableView content to change and update dynamically as new MyObject of this type are added? For this we require a NSFetchedResultsController right?

Here is my code for loading ALL MyObject's into a UITableView (which works):

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"MyObject" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                          initWithKey:@"creationDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                    managedObjectContext:managedObjectContext sectionNameKeyPath:@"transientSectionDate" 
                                               cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return _fetchedResultsController;    

}

Could someone PLEASE be as so kind to show my what actual NSPredicate declaration I need to correctly load ONLY MyObject's whose MyObjectType.name == @"XXXXXX"? Let's assume I already have a MyObjectType.name stored in a retained NSString inside the ViewController.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无戏配角 2024-10-28 21:59:23

谓词格式字符串将为:

@"ALL type.name=%@", typeName

但是,由于您确实拥有特定的 MyObjectType 对象,因此您已经可以直接访问所需的 MyObject 对象,而不必浪费时间尝试获取它们。只需将集合转换为排序数组即可。

要在表处于活动状态时随时了解正在进行的更改,请在表视图数据源对象中实现observeValueForKeyPath:ofObject:change:context:。然后将 addObserver:forKeyPath:options:context: 发送到特定的 MyObjectType 对象,如下所示:

[anObjectType addObserver:self 
               forKeyPath:@"objects" 
                  options:(NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld) 
                  context:nil];

现在,每当该特定 MyObjectType 的 objects 值发生更改时,tableview 的数据源都会收到通知并且可以改变桌子。

请参阅 键值观察编程指南了解详细信息。

The predicate format string would be:

@"ALL type.name=%@", typeName

However, since you do have a particular MyObjectType object, you already have direct access to the needed MyObject objects and don't have to waste time trying to fetch them. Just convert the set into a sorted array.

To keep apprised of ongoing changes while the table is active, implement observeValueForKeyPath:ofObject:change:context: in the tableview datasource object. Then send addObserver:forKeyPath:options:context: to that particular MyObjectType object like so:

[anObjectType addObserver:self 
               forKeyPath:@"objects" 
                  options:(NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld) 
                  context:nil];

Now, whenever the objects value of that paticular MyObjectType changes, the tableview's datasource will be notified and can change the table.

See Key-Value Observing Programming Guide for details.

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