1

I want to show Project such that it shows all the projects on the basis of sharedToUsers basis. Means section name with User.user_id and it should shows all the shared project with that user. but i am not able to set section value for NSFetchedResultsController properly because it is a too many relation and it is crashing on sectionNameKeyPath . Any Help

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *theParent = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:_managedObjectContext];
//the name key exsit in parent entity of project.
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:10];
[fetchRequest setEntity:theParent];

[fetchRequest setPredicate:_predicate];

  NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:_managedObjectContext sectionNameKeyPath:@"ANY sharedToUsers.user_id"
                                               cacheName:[self getCacheNameForNSFetchedResultsController]];

to-many relation in coredata

Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83
  • This might help: http://stackoverflow.com/a/17136386/1187415. – Martin R Dec 11 '14 at 08:50
  • cannot change the design of database now, i am near to complete the project. but its a good idea in the given link – Iqbal Khan Dec 11 '14 at 08:57
  • Well, as mentioned in the link and also in below comments, a FRC cannot fetch the same object multiple times, so you are out of luck here. I do not know another workaround (apart from creating multiple FRCs, one for each section, which is a bit nasty to manage). – Martin R Dec 11 '14 at 09:01

2 Answers2

0

if you have a senario like i have then when ever you change data in the core data then try donot use to add or update relation ship items using object of parent or category object. if you had to link some thing with category then try to link from child object.

donot use this

[category addRelationShipItem:child];

use this

`[child addToCategory:category];`

when you use this then it will not triger update call in NSFetchResultController call.

Happy coding.

Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83
-1

Hi If you want fetch the Project shared by a user you need this predicate:

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *theParent = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:_managedObjectContext];

   NSString *userId; // If you user_id is a NSNumber change you to it.
   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY sharedToUsers.user_id = %@",userId];
   [fetchRequest setPredicate:predicate];

Last things you are using a NSSort: name , but name it's not a Project property.

Onik IV
  • 5,007
  • 2
  • 18
  • 23
  • i want sections on the basis of sharedToUsers.user_id. means i want to show all the projects in sections that are shared to different users – Iqbal Khan Dec 11 '14 at 08:20
  • OP asked specifically about sections and there is nothing about them in your answer – Michał Ciuba Dec 11 '14 at 08:23
  • @Developer But If you relationships is to many, a project could be in several sections (repeated), as far as I know, that is no posible using this NSFetchedResultsController. If a Project could be shared by several users (and as you have this relationship is to many). You need fetch users (no Project) and show its sharedProjects. – Onik IV Dec 11 '14 at 08:34
  • If you want, you can created a custom key to order projects, and show a Project by the first user have shared.(or another thing). But You can't show the same project in several users. – Onik IV Dec 11 '14 at 08:42
  • yes project can be multiple times in different users. do you think it is not possible with NSFetchedResultsController – Iqbal Khan Dec 11 '14 at 08:45
  • 1
    No, In general when you do a request you can't fetch the same object several times. But you can ask for users, using a Predicate than They have shared at least a project. Use this request for sections, and for row a section user.sharedPrijectsFrom... with the sort you want. – Onik IV Dec 11 '14 at 08:50
  • In fact your keyPath must be: sharedToUsers.user_id, I think the exception look something like this is to many and no posible. – Onik IV Dec 11 '14 at 08:52
  • I would go with @OnikIV suggestion that I voted up - fetch Users in your FRC and amend tableView data source methods to populate sections based on the User object and cells based on the sharedProjectsFrom for that User object – pbasdf Dec 11 '14 at 09:43
  • @OnikIV your comment actually answers the question, maybe include it in the answer – Michał Ciuba Dec 11 '14 at 19:47
  • @MichałCiuba That´s the important things. We all use this platform to improve and learn. – Onik IV Dec 11 '14 at 20:42