我可以将 NSFetchedResultsController 放入托管对象类中吗?

发布于 2024-10-15 00:26:31 字数 1995 浏览 3 评论 0原文

我厌倦了不断地在我使用托管对象上下文的几乎每个文件中放置/重复我的项目的 NSFetchedResultsController 代码。

我想减少重复代码的数量。我想将这种类似 CRUD 的重复代码放入模型类中。

我想要的是将所有自定义 NSFetch 放入相关实体的托管对象类中(即:Company.m、Employee.m)。

即使 Apple 的 Core Books 示例代码也没有将此代码放入托管对象类中,我想知道这是否可能?

我尝试将代码粘贴到我的 Company.m 类中,但它一直抱怨 ManagedObjectContext 并且还抱怨 fetchedResultsController 未声明,即使它是一个参数?

理想情况下,我也想将许多不同类型的获取请求/结果控制器内容放入实体管理对象类中。

但在我超出自己之前,是否有可能将所有 NSFetchedResultsController 内容放入实体管理对象类中?

如果有一个涵盖此内容的示例教程、项目或源代码,那就太好了。

谢谢。

(代码示例如下)。

/**
 Returns the fetched results controller. Creates and configures the controller if necessary.
 */
- (NSFetchedResultsController *)fetchedResultsController
{

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

    // Create and configure a fetch request with the Book entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [authorDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    

I'm sick and tired of constantly having to putting/repeat the NSFetchedResultsController code for my project in virtually every file where I'm working with the Managed Object Context.

I want to reduce the amount of repetitive code. I want to put this kind of repetitive CRUD-like code inside the model class.

What I want instead is put all my custom NSFetchs inside the Managed Object Class for the relevant Entity (ie: Company.m, Employee.m).

Even the Core Books sample code from Apple does not put this code into the Managed Object Class and I'm wondering if its possible?

I tried pasting the code into my Company.m class but it keeps complaining about the managedObjectContext and also it complains that fetchedResultsController is not declared, even though its a parameter?

Ideally, I would like to put lots of different kinds of fetch request/results controller stuff inside the Entity Managed Object Class too.

But before I get ahead of myself, Is it possible, therefore, just to put all the NSFetchedResultsController stuff inside the Entity Managed Object class?

If there is a sample tutorial or project or source code that covers this, that'd be great too.

Thanks.

(code sample follows).

/**
 Returns the fetched results controller. Creates and configures the controller if necessary.
 */
- (NSFetchedResultsController *)fetchedResultsController
{

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

    // Create and configure a fetch request with the Book entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [authorDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    

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

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

发布评论

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

评论(1

霓裳挽歌倾城醉 2024-10-22 00:26:31

我建议将 ActiveRecord 与 xmod 结合使用。如果您修改核心数据模型,CoreData 将覆盖您的 CRUD。 ActiveRecord 使其变得与调用 [MyManagedObject createEntity];NSArray *myObjects = [MyManagedObject findAll]; 一样简单。还有一些选项可以传递谓词来过滤 findAll 调用。添加 xmod 会生成生成类的子类,以便您可以向实体添加自定义逻辑,以免它们被覆盖。

编辑:我想添加到此 Active Record 实现,因为这是我实际使用的。

Edit2:现在已重命名为 Magical Record

I recommend using ActiveRecord with xmod. CoreData will overwrite your CRUD if you modify your core data model. ActiveRecord makes it as easy as calling [MyManagedObject createEntity]; and NSArray *myObjects = [MyManagedObject findAll]; There is also options to pass predicates to filter the findAll call. The xmod addition generates a subclass to the generated classes so that you can add custom logic to your entities so that they do not get overridden.

Edit: I would like to add the link to this Active Record implementation since this is the one I actually use.

Edit2: This has now been renamed to Magical Record.

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