Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 9.70 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why isn't the data from my coredata model loading in the bottom view?
  2. - (NSFetchedResultsController *)fetchedResultsController {
  3.  
  4. // If the fetched results controller already exists, return it
  5. if (fetchedResultsController_ != nil) {
  6.     return fetchedResultsController_;
  7. }
  8.  
  9. // Otherwise, create it, configure it, and return it
  10.  
  11. // Configure the enitity, and attach it to a managed object context
  12. NSEntityDescription *entity =
  13. [NSEntityDescription entityForName:@"Top" inManagedObjectContext:self.managedObjectContext];
  14.  
  15. // Create the fetch request
  16. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  17.  
  18. // Set the fetch request's entity property
  19. [fetchRequest setEntity:entity];
  20.  
  21. /** Set the batch size to a suitable number.
  22.    */
  23. [fetchRequest setFetchBatchSize:20];
  24.  
  25. // Create sort descriptor(s)
  26. // We will use two here, because we want to group the programs by credential
  27. NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"topViewPosition" ascending:YES];
  28. NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"topSecName" ascending:YES];
  29. NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
  30.  
  31. // Set the fetch request's sortDescriptors property
  32. [fetchRequest setSortDescriptors:sortDescriptors];
  33.  
  34. // For this root view controller, in this application, we want ALL the Programs
  35. // Therefore, we will leave out the predicate, which will fetch all the results
  36.  
  37. // Clear the cache
  38. [NSFetchedResultsController deleteCacheWithName:@"Top"];
  39.  
  40. // Now, create the fetched results controller
  41. // Edit the section name key path and cache name if appropriate; nil for section name key path means "no sections"
  42. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
  43.                                         initWithFetchRequest:fetchRequest
  44.                                     managedObjectContext:self.managedObjectContext
  45.                                          sectionNameKeyPath:@"topSecName" cacheName:@"Top"];
  46.  
  47. // Set the fetched results controller's delegate to self; it will call controllerDidChangeContent:
  48. aFetchedResultsController.delegate = self;
  49. // Assign this new fetched results controller to the property
  50. self.fetchedResultsController = aFetchedResultsController;
  51.  
  52. // Memory manage
  53. [aFetchedResultsController release];
  54. [fetchRequest release];
  55. [sortDescriptor1 release];
  56. [sortDescriptor2 release];
  57. [sortDescriptors release];
  58.  
  59. return fetchedResultsController_;
  60.        
  61. - (NSFetchedResultsController *)fetchedResultsController {
  62.  
  63. // If the fetched results controller already exists, return it
  64. if (fetchedResultsController_ != nil) {
  65.     return fetchedResultsController_;
  66. }
  67.  
  68. // Otherwise, create it, configure it, and return it
  69.  
  70. // Configure the enitity, and attach it to a managed object context
  71. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Middle"
  72.                                           inManagedObjectContext:[referringObject_ managedObjectContext]];
  73.  
  74. // Create the fetch request
  75. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  76.  
  77. // Set the fetch request's entity property
  78. [fetchRequest setEntity:entity];
  79.  
  80. /** Set the batch size to a suitable number.
  81.  */
  82. [fetchRequest setFetchBatchSize:20];
  83.  
  84. // Create sort descriptor(s)
  85. NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"middleViewPosition" ascending:YES];
  86. NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"middleSecName" ascending:YES];
  87. NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
  88.  
  89. // Set the fetch request's sortDescriptors property
  90. [fetchRequest setSortDescriptors:sortDescriptors];
  91.  
  92. // Predicate configuration - specify the relationship's property name "top"
  93. // checks whether the value of the key "programs" is the same as the value of the object %@
  94. NSPredicate *pred = [NSPredicate predicateWithFormat:@"top == %@", self.referringObject];
  95. [fetchRequest setPredicate:pred];
  96.  
  97. // Clear the cache
  98. [NSFetchedResultsController deleteCacheWithName:@"Middle"];
  99.  
  100. // Create the fetched results controller
  101. // Edit the section name key path and cache name if appropriate; nil for section name key path means "no sections"
  102. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
  103.                                                 initWithFetchRequest:fetchRequest
  104.                                             managedObjectContext:[referringObject_ managedObjectContext]
  105.                                                 sectionNameKeyPath:@"middleSecName" cacheName:@"Middle"];
  106.  
  107. // Set the fetched results controller's delegate to self; it will call controllerDidChangeContent:
  108. aFetchedResultsController.delegate = self;
  109. // Assign this new fetched results controller to the property
  110. self.fetchedResultsController = aFetchedResultsController;
  111.  
  112. // Memory manage
  113. [aFetchedResultsController release];
  114. [fetchRequest release];
  115. [sortDescriptor1 release];
  116. [sortDescriptor2 release];
  117. [sortDescriptors release];
  118.  
  119. return fetchedResultsController_;
  120.        
  121. - (NSFetchedResultsController *)fetchedResultsController {
  122.  
  123. // If the fetched results controller already exists, return it
  124. if (fetchedResultsController_ != nil) {
  125.     return fetchedResultsController_;
  126. }
  127.  
  128. // Otherwise, create it, configure it, and return it
  129.  
  130. // Configure the enitity, and attach it to a managed object context
  131. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bottom"
  132.                                           inManagedObjectContext:[referringObject_ managedObjectContext]];
  133.  
  134. // Create the fetch request
  135. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  136.  
  137. // Set the fetch request's entity property
  138. [fetchRequest setEntity:entity];
  139.  
  140. /** Set the batch size to a suitable number.
  141.  */
  142. [fetchRequest setFetchBatchSize:20];
  143.  
  144. // Create sort descriptor(s)
  145. NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"bottomViewPosition" ascending:YES];
  146. NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
  147.  
  148. // Set the fetch request's sortDescriptors property
  149. [fetchRequest setSortDescriptors:sortDescriptors];
  150.  
  151. // Predicate configuration - specify the relationship's property name "middle"
  152. // checks whether the value of the key "programs" is the same as the value of the object %@
  153. NSPredicate *pred = [NSPredicate predicateWithFormat:@"middle == %@", self.referringObject];
  154. [fetchRequest setPredicate:pred];
  155.  
  156. [NSFetchedResultsController deleteCacheWithName:@"Bottom"];
  157.  
  158. // Create the fetched results controller
  159. // Edit the section name key path and cache name if appropriate; nil for section name key path means "no sections"
  160. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
  161.                                                         initWithFetchRequest:fetchRequest
  162.                                                     managedObjectContext:[referringObject_ managedObjectContext]   sectionNameKeyPath:@"bottomSecName" cacheName:@"Bottom"];
  163.  
  164. // Set the fetched results controller's delegate to self; it will call controllerDidChangeContent:
  165. aFetchedResultsController.delegate = self;
  166. // Assign this new fetched results controller to the property
  167. self.fetchedResultsController = aFetchedResultsController;
  168.  
  169. // Memory manage
  170. [aFetchedResultsController release];
  171. [fetchRequest release];
  172. [sortDescriptor1 release];
  173. [sortDescriptors release];
  174.  
  175. return fetchedResultsController_;
  176.        
  177. // This method is called when the user taps/clicks the row to drill down to the next level
  178.        
  179. // Get the object for the selected row
  180. // Note that we don't own the object that we get from the fetched results controller
  181. // Therefore, we won't release it (memory management) later
  182. NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
  183.  
  184.     // Create the next-level view controller
  185. MiddleList *nextLevelListVC = [[MiddleList alloc] initWithNibName:@"MiddleList" bundle:nil];
  186.  
  187. // Set the next-level view controller's display title
  188. nextLevelListVC.title = [selectedObject valueForKey:@"topName"];
  189.  
  190. // Configure the next-level view controller with the selected object
  191. // We do this here because the next-level view controller is a drill-down table view
  192. // We may need the selected object to set or follow a Core Data entity relationship
  193. nextLevelListVC.referringObject = selectedObject;
  194.  
  195. // Remember that the managed object context is a property of the referringObject above...
  196.  
  197. // Push the next-level view controller onto the navigation stack
  198. [self.navigationController pushViewController:nextLevelListVC animated:YES];
  199.  
  200. // Manage memory
  201. [nextLevelListVC release];
  202.        
  203. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  204.  
  205. // Get the object for the selected row
  206. // Note that we don't own the object that we get from the fetched results controller
  207. // Therefore, we won't release it (memory management) later
  208. NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
  209.  
  210. // Create the next-level view controller
  211. BottomList *nextLevelListVC = [[BottomList alloc] initWithNibName:@"BottomList" bundle:nil];
  212.  
  213. // Set the next-level view controller's display title
  214. nextLevelListVC.title = [selectedObject valueForKey:@"middleName"];
  215.  
  216. // Configure the next-level view controller with the selected object
  217. // We do this here because the next-level view controller is a drill-down table view
  218. // We may need the selected object to set or follow a Core Data entity relationship
  219. nextLevelListVC.referringObject = selectedObject;
  220.  
  221. // Remember that the managed object context is a property of the referringObject above...
  222.  
  223. // Push the next-level view controller onto the navigation stack
  224. [self.navigationController pushViewController:nextLevelListVC animated:YES];
  225.  
  226. // Manage memory
  227. [nextLevelListVC release];