开发者

How to refresh a TableView when data is inserted in the AppDelegate?

To pre-populate my Database for a small Iphone-App I do a check in the persistentStoreCoordinator Method in the AppDelegate if the sqlite-file is already there or not. If not I add some data into a table after the db is created. But the data should not directly shown in the RootViewController but in another TableView which can be accessed by pressing a Option in the RootViewController.

The Problem is that the data are saved but are not shown in the TableView. But if terminate and restart the app the data are there.

I have passed the managedObjectContext from my appDelegate to the the RootViewController and from there to the TableViewController. So the managedObjectContext is the same everywhere.

Here is the Code where I pre-populate the DB (in this example just one sample row):

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
  开发者_JAVA百科  return persistentStoreCoordinator_;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestDBApp.sqlite"];

NSString *storePath = [storeURL relativePath];
BOOL noDb = false;

NSFileManager *fileManager = [NSFileManager defaultManager];
if( ![fileManager fileExistsAtPath:storePath]) {
    noDb = true;
}

NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

if( noDb) {
    noDb = false;
    [self populateDB];
}

return persistentStoreCoordinator_;

}

- (void) populateDB {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Kunde" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"oxid" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

[self insertNewKunde:aFetchedResultsController];

[self saveContext];

[aFetchedResultsController release];
[sortDescriptors release];
[sortDescriptor release];
[fetchRequest release];

}

- (void)insertNewKunde:(NSFetchedResultsController *)fetchedResultsController {

// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

// If appropriate, configure the new managed object.
[newManagedObject setValue:[[NSDate date] description] forKey:@"oxid"];
[newManagedObject setValue:@"Max" forKey:@"fname"];
[newManagedObject setValue:@"Mustermann" forKey:@"lname"];

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

}

The managedObjectContext is passed from the AppDelegate to the RootViewController as:

- (void)awakeFromNib {

RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];

rootViewController.managedObjectContext = self.managedObjectContext;

}

and to the other TableViewController:

- (void)awakeFromNib {
kundenVC = [[KundenViewController alloc] initWithManagedObjectContext:self.managedObjectContext];

}

Can anyone help me or give a hint?

I also tried to reload the tableView through the viewWillAppear-Method. But no effect.


It is very difficult to guess what is happening but my hunch is that you are not calling self.populateDB until the RootViewController is actually created. This means the RootViewController does not see that data because it is not created. I could be wrong but I guess that your last line in delegate's awakeFromNib method is eventually calling populateDB through self.managedObjectContext -> persistentStoreCoordinator -> self.populateDB.

Please try to add a line right at the top of awakeFromNib looking like this:

(void)awakeFromNib {
     NSManagedObjectContext *context = self.managedObjectContext;

     RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];

     rootViewController.managedObjectContext = context;}

This way I hope the your problem is solved because self.managedObjectContext will call the getter (- (NSManagedObjectContext)managedObjectContext;) instead of the field. Inside there (if you use the generated code) there is a call to the getter of ** persistentStoreCoordinator** which eventually calls you populateDB method.

In case that does not work go ahead and set a breakpoint inside your populateDB. When the debugger stops you should see at the method call stack where you populateDB is actually called from and that might help to figure out your problem.

-Andy


Well, the important part you left out to answer your question. What is [self saveContext] and how are you displaying the records in your table view.

Because your data is saved I assume that the code given here works. The only guess I can make is to use reset on the NSManagedObjectContext and then refetch.

BTW I like the "insertNewKunde" which seems to be the new German grammar ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜