views:

349

answers:

1

Hi guys,

I need some guidance with this issue I have. I have a navigational Controller, with the root controller making use of CoreData and an NSFetchController. Now, the root controller works fine. When I click on an item, hierarchically it should display the next UITable associating the values for the row in the previous root controller.

Now, I am not sure what I should pass as a property to the next UITableViewController. Should it be an array, or should I pass the NSFetchedResultsController? THere will be another level in the hierarchy after the second, as a point of note.

Thanks Doron

+2  A: 

You have a couple options, at least:

  1. Pass a reference to the managed object controller from parent view controller to child view controller; or,
  2. Make the MOC property available from the app delegate, which is available to any view controller

Some prefer the second option as you may reuse and rearrange view controllers into different hierarchies. By disconnecting the MOC from parent and child controllers, you gain a bit more design flexibility. Also, if your application needs to manage more than one persistent store concurrently, then getting access from one central point to make multiple MOCs reduces code complexity.

Once you have the MOC in its view controller, you can create your fetched results controller in that view controller and apply operations in their own threads.

EDIT

It sounds like you have a one-to-many relationship between Site and Post (i.e. one site entity has many post entities).

Your "Posts" list view controller could include a site managed object, siteID managed object ID, or a siteName string property.

When you push the "Posts" list VC, set its site or siteName property and configure the request predicate accordingly.

For example, your fetch request's predicate should include something like site.name LIKE '%@' in it, e.g.:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(site.name like '%@')", self.site.name]];
[fetchRequest setPredicate:requestPredicate];
// ...

Or you could compare on managed object IDs, which is generally better for unique comparisons:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(site = %@)", self.siteID]];
[fetchRequest setPredicate:requestPredicate];
// ...

In both cases, you have filtered your Core Data store to match all Post entities that have a site name or a site managed object ID equivalent to your criteria.

Once you perform the fetch, these entities generally reside in an NSSet* or NSArray* that you can easily use as a UITableView data store, especially if you are using a fetched results controller.

Alex Reynolds
Thanks Alex, I sort of tried doing that, as I am making use of an AppController where i pass between controllers the MOC amongst other things. Doing it this approach (creating a new fetchrequest), i am not sure how i link the entity based on result of previous (i.e from Site -> Posts, how do i do a predicate to relate to specific selected Site).Thats what i was getting at.Thanks mate
Doron Katz
One more thing mate, Just a note, my model has Site property which has an NSSET of many posts (which is why i had the idea for an NSArray to be passed) but yeah im not sure which way I should realistically proceed. You recommend using MOC, but if i can get some clarification on how it is used, to map based on selected Site.
Doron Katz
I currently am playing with the MOC, using NSPredicate *pred = [NSPredicate predicateWithFormat:@"(site = %d)", i]; Where i is the site entity object i want it to be compared to. Obviously %d isnt the correct syntax, so how do i fix this if i were to go with MOC?
Doron Katz
Hi mate, thanks for your detailed response. Now ive implemented what you have said, minus the predicate and get rows back. Now using the predicate you recommended, Site.name it doesnt work , so i use the property in Post.h, called Site sitePosts and use (PostSite.name like '%@'), [self.site.name], but get no rows back. I this my predicate is wrong, but not sure what im doing wrong mate
Doron Katz
Not sure what's wrong. Maybe you could post a screenshot of your data model so that we both use the same object names, and you could also add the code where you initialize your fetched results controller (in your posts view controller).
Alex Reynolds
Hi Alex, OK. Ive posted the model screenshot and code snippet for the Posts controller to https://dl.dropbox.com/u/2568964/stackoverflow/model.png and https://dl.dropbox.com/u/2568964/stackoverflow/posts-fetchcontroller.png respectively. Hope that helps in helping me lol. Thanks mate
Doron Katz
OMG hey Alex just realised my predicate line was stuffed up. Hmm.. Im getting it now, I had the site.name in the wrong place. Gosh duh!
Doron Katz