Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing objects in CoreData
    text
    copied!<p>I am using CoreData in my application and it is a one-to-many relationship, I have two entities called <code>folders</code> and <code>files</code>. So a folder can have many files. So my first view controller shows all folders and when clicked on each folder, I show the files present in it. </p> <p>Now all the folders and files present in it are created by user dynamically and are not coming from any database or from web server.</p> <p>Now I have one more view controller, which I open from app delegate, and here I want to show some files.</p> <p>I know that first we should pass the <code>managedObjectContext</code>, which will be declared in app delegate</p> <pre><code>ViewController *View = [[ViewController alloc]initWithNibName: @"ViewController" bundle:nil] View.managedObjectContext = self.managedObjectContext; </code></pre> <p>So if we just pass <code>managedObjectContext</code>, can we access the file entity objects, OR we have to pass file entity object also.</p> <p>Now as you know, I have two entities, called <code>Folder</code> and <code>File</code>, now to access files stored in any folder, I just do this.</p> <pre><code>NSMutableArray *filesArray = [self.folder.file mutableCopy]; </code></pre> <p>But now, I want to show the files in anyView I want, which can be opened after two three navigations or it is opened directly from appDelegate.m.</p> <p>To be very clear , my question is how to do this? i.e how can I get filesArray from <code>AppDelegate</code>.</p> <p>Based on Below Answer edited. I have created a fetchResultsController this way </p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController { if (m_fetchResultsController != nil) { return m_fetchResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"File" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"alarm" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"alarm!= nil"]]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; aFetchedResultsController.delegate = self; self.fetchResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchResultsController performFetch:&amp;error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return m_fetchResultsController; } </code></pre> <p>and in ViewDidLoad and I written this way </p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; NSError *error; if (![[self fetchedResultsController] performFetch:&amp;error]) { // Update to handle the error appropriately. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); exit(-1); // Fail } NSMutableArray *alarms = [[self.fetchResultsController fetchedObjects]mutableCopy]; NSLog(@"alarmsCount:%d",[alarms count]); } </code></pre> <p>Regards Ranjit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload