I would recommend using an NSFetchedResultsController
in each view controller. That way you don't have to deal with arrays in every view controller, only fetch requests. I would not recommend creating them in the App Delegate though. Preferably in each view controller or in a category.
So in your view controller try creating a property for an NSFetchedResultsController
and implement its getter.
NSFetchedResultsController example
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- (NSFetchedResultsController *)fetchedResultsController
{
if (!_fetchedResultsController) {
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:[self fetchRequest]
managedObjectContext<# your managedObjectContext #>
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController.delegate = self;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Error: %@ %@", error.localizedDescription, error.userInfo);
}
}
return _fetchedResultsController;
}
and implement a method for your fetch request:
- (NSFetchRequest *)fetchRequest
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MMU"];
// predicates, sort descriptors and other options go here...
return fetchRequest
}
And then you can update your tableview data source by using
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.fetchedResultsController.fetchedObjects.count;
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
MMU *results = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@ ",results.name]; // Consider results.name without stringWithFormat.
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ ", results.timeStamp];
return cell;
}
Now after you set up your view controller like this you update the model when you touch or place text somewhere. In your next view controller you can set up a new fetched results controller and a new fetch request, requesting the already changed data.
Depending on the number of view controllers you have, a better approach would be to subclass your UITableViewController to implement a fetched results controller by default and change the fetch request each time.
If this didn't help, please provide some more information about how you present your new view controllers and how you update your model.
Update
You can pass each object from view controller to view controller by implementing either prepareForSegue:
or tableView:didSelectRowAtIndexPath:
. In this case you will have to declare a public property in your header file for each view controller you want to pass the object to. Please note that implementing these methods you will be navigated to the next view controller by tapping a cell and not by the IBAction
you provided in your example.
If you are using segues for pushing view controllers you can use the following example.
prepareForSegue:
Example
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
UIViewController *nextViewController = segue.destinationViewController;
nextViewController.mmuModel = self.mmuModel; // The initial model that you have from InfoViewController.
}
This method will share your mmuModel with the next view controller. However you will need to update the model in each view controller separately.
This will get the job done, however CoreData is not meant to be used in such a way. CoreData is a powerful framework which usually does a lot of stuff for you. Another approach is to create a new NSManagedObject
in InfoViewController
like you do and then instead of passing the managed object in the question view controllers you can fetch it each time using an NSFetchedResultsController
like I showed you. Only this time you will have to figure out a way of filtering the results so you can get the object you are looking for. There are multiple ways to do this, one approach would be to use some sort of id and maybe a predicate.
Moreover in your data model, instead of having everything in one entity (MMU), consider separating some things and using relationships.
Also it would be a good practive to different data types for your attributes i.e. timeStamp should be a Date, gender could be a BOOL if the only options are male, female, etc etc.
I hope this helped a bit. Let me know if you're still having issues.