Core Data / NSTextView outlets not updating after fetching (OS X)

164 Views Asked by At

I am new to this, so if I come off a tad "dense" I apologize.

I'm writing a core data application. On my .xib, I have several outlets including text fields, date pickers and "Text Views" using the NSTextContainers. These are set up correctly.

My application is a makeshift database of sorts. When a user saves a record, clears the fields, and immediately tries to reload the record, all of the fields populate except for the NSTextView. Now, if I close the application out completely, restart it, and then retrieve the record, the NSTextContainers (NSTextView) outlets operate correctly and show the correct data. Its only when you immediately try to load a record back that there is a problem and the outlets appear blank. I set up some NSLog statements to see if the data was being retrieved and lo and behold, the fetch is not getting the NSTextView object data. It's coming back blank.

Here are some code snippets. I would appreciate ANY help as this has been driving me crazy.

    @property (weak) IBOutlet NSTextField *clientnameField;
    @property (unsafe_unretained) IBOutlet NSTextView *clientaddressField;
    @property (weak) IBOutlet NSTextField *clientphoneField;
    @property (unsafe_unretained) IBOutlet NSTextView *clientnotesField;





    - (IBAction)saveButton:(id)sender {


      AppDelegate *appDelegate = (AppDelegate*) [[NSApplication sharedApplication]delegate];

         NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:appDelegate.managedObjectContext];


                      [entity setValue:self.clientnameField.stringValue forKey:@"clientname"];
            [entity setValue:self.clientaddressField.string forKey:@"clientaddress"];
            [entity setValue:self.clientphoneField.stringValue forKey:@"clientphone"];
            [entity setValue:self.clientnotesField.string forKey:@"clientnotes"];


                 NSError *error;

            BOOL isSaved = [appDelegate.managedObjectContext save:&error];


    }



  //Fetching the Data Back  

  - (void)alertDidSearch:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {  

    if (returnCode == NSAlertSecondButtonReturn) {


                       AppDelegate *appDelegate = (AppDelegate*) [[NSApplication sharedApplication]delegate];



            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Team" inManagedObjectContext:appDelegate.managedObjectContext];



            NSFetchRequest *fetchRqst = [[NSFetchRequest alloc] init];
               [fetchRqst setEntity:entity];


            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(clientname == %@)", self.clientnameField.stringValue];
                    [fetchRqst setPredicate:predicate];



            NSMutableArray *array = [[appDelegate.managedObjectContext executeFetchRequest:fetchRqst error:nil]mutableCopy];



            if ([array count] == 0) {



                NSAlert *notFoundAlert;



                notFoundAlert = [[NSAlert alloc] init];

                [notFoundAlert addButtonWithTitle:@"OK"];

                [notFoundAlert setMessageText:@"RECORD NOT FOUND!!"];

                [notFoundAlert setInformativeText:@"The client was not found. Check the name and spelling and try again."];

                [notFoundAlert setAlertStyle:NSWarningAlertStyle];

                [notFoundAlert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEndOK:returnCode:contextInfo:) contextInfo:nil];


                } else {



                for (NSManagedObject *obj in array)

                {

                    self.clientnameField.stringValue = [obj valueForKey:@"clientname"];

                    self.clientaddressField.string = [obj valueForKey:@"clientaddress"];

                    self.clientphoneField.stringValue = [obj valueForKey:@"clientphone"];

                    self.clientnotesField.string = [obj valueForKey:@"clientnotes"];



                }

            }

    }

The client address field and client notes fields (outlets) are not being retrieved.

I'm not sure what I'm doing wrong. Again, the data IS being saved, because when I immediately retrieve the record, every field populates except the clientnotes and the clientaddress fields which are again, NSTextView outlets. Those do not show data until I exit out, restart the program and then retrieve the record. If I had to guess, and I am guessing, I assure you, it seems like the persistent store isn't being updated with respect to any .string(s). In fact, if I add multiple records and try to retrieve multiple records before exiting the program, they all exhibit the same issue. Closing the program, restarting it, and re-fetching the records and it's all fine.

Thank you so much in advance for the help. I am stumped. Again, I apologize if this is a stupid question... Please have mercy on me! And an apology for the long winded explanation, but I wanted to be as clear as possible.

EDIT:

It definitely appears as if the NSTextView data isn't being written until the file is closed out when the program terminates.

How can I fix this? My project is at a standstill...... :-(

0

There are 0 best solutions below