Question

I am new in programming in ios and I'm creating this simple function of having a view controller add a data into the persistent store, and a table view controller will fetch those data and display them in rows.

This data that I'm adding is a string that comes from UITextField and I've connected IBAction to the Add button, which takes text for UITextField and add it in.

I'm able to observe new data from being added in after I restart my iOS simulator. However, I'm not able to see the change on-the-fly (within the same test run).

May I know if I'm supposed to close and re-open the iOS simulator everytime (this is not the user experience I would like for my users), and is there anyway to solve?

Thank You!

Here's my IBAction code in my ViewController:

- (IBAction)addFriendEntry:(id)sender {
NSManagedObjectContext *moc = [self managedObjectContext];
NSManagedObject *newFriend = [NSEntityDescription
                              insertNewObjectForEntityForName:@"Player"
                              inManagedObjectContext:moc];

[newFriend setValue:self.friendNameTextField.text forKey:@"name"];

NSLog(@"You are trying to add this friend:%@", self.friendNameTextField.text);

NSError *error;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Oops, couldn't save: %@", [error localizedDescription]);
}

self.friendNameTextField.text = @"";

[self.view endEditing:YES];
}

Here's my code in my TableViewController:

- (void)viewDidLoad
{
[super viewDidLoad];
self.Friends = [[NSMutableArray alloc] init];

PCRAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
self.Friends = [appDelegate fetchFriendsFromDataBase];

//Some small test to tell me that the app is responding to me everytime I switch to the   TableViewController
NSLog(@"hi from tableview");

[self.tableView reloadData];

}

I have some other methods in my AppDelegate.h which includes:

- (NSManagedObjectContext *)managedObjectContext;
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
- (NSManagedObjectModel *)managedObjectModel;

- (NSArray *)fetchFriendsFromDataBase;

My fetchFriendsFromDataBase methods is:

- (NSArray *)fetchFriendsFromDataBase

{
//Takes in name of friend. Using this name, which is an attribute, find the entity from persistent store and pull out the NSManagedObject.
//initializing NSFetchRequest
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//Sets Entity to be queried
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSError *error;

//Execute Fetch Request
NSArray *fetchedPlayers = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

return fetchedPlayers;
}

I still couldn't see any changes whenever I click add to trigger the IBAction.

Was it helpful?

Solution 2

Thanks guys. I found my answer.

viewDidLoad is called when the app has just ran.

I have to put my code into viewDidAppear instead.

OTHER TIPS

After fetching the new data, you need to reload your tableview so it shows the new data. I'm assuming you connected the delegate and datasource and are dynamically setting the numberOfRowsInSection:

If you never created a tableview before: http://www.guilmo.com/how-to-create-a-simple-uitableview-with-static-data/

If you are, then after fetching the data try [tableView reloadData];

This should refresh the tableview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top