Mac OS X replacement for NSFetchedResultsController
Asked Answered
J

1

8

So I'm used to iOS development so I'm pretty happy with NSFetchedResultsController. However, this is not present in the Mac OS X environment. I know that I can use NSArrayController as a replacement. I might be using this class terribly wrong but it worked until now. I initialize the NSArrayController as follows:

NSArrayController* newConversationsController = [NSArrayController new];
newConversationsController.managedObjectContext = context;
newConversationsController.entityName = entityName;
newConversationsController.sortDescriptors = sortDescriptors;
newConversationsController.automaticallyRearrangesObjects = YES;

NSError* error = nil;
[newConversationsController fetchWithRequest:nil merge:NO error:&error];
NSCAssert(!error, error.description);

Then I listen to changes of the NSManagedObjectContext and fetch and reload the NSTableView as follows:

        [self.conversationsController fetchWithRequest:nil merge:YES error:&error];
    NSAssert(!error, error.description);

    [self.tableView reloadData];

As I previously mentioned, I might be using this totally wrong but I don't like to use bindings. Now to the actual issue: Another class of the application might delete an NSManagedObject held by the NSArrayController. The NSArrayController instantly releases this deleted object and makes it impossible for me to figure out which object that was. The final goal is to know what object at what index has been deleted so I can animate the rows of the NSTableView.

I hope it's clear what I'm aiming at. Thanks for any help

Janenejanenna answered 19/1, 2014 at 10:33 Comment(2)
Why don't you like bindings?Ethelda
Well the NSTableCellViews are rather custom. Wouldn't make that a lot harder to use bindings then?Janenejanenna
A
3

there are no sections in a NSTableView so there is no 1:1 replacement

BUT NSArrayController is a close match! It also fetches and sorts CoreData entities.

For the UI you'd use bindings then (but of course you don't have to)


e.g. see:

http://cocoadevcentral.com/articles/000080.php

Alcyone answered 19/1, 2014 at 11:7 Comment(3)
note that you don't have to use bindings! a NSArrayController can of course be used directlyAlcyone
Something like an NSOutlineView might be thought of as a closer substitute for a UITableview with sections.Neodymium
yes which he doesn't want ;) I merely referenced sections to say there is no ios-like fetchedResultsControllerAlcyone

© 2022 - 2024 — McMap. All rights reserved.