Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone - core data issue when adding
    text
    copied!<p>It is something simple but I just can't seem to find the problem. New elements aren't getting added into the database and I'm not getting any errors in my code.</p> <p>Here is the add method</p> <pre><code>- (void) addPressed:(NSNotification *) message { // Insert a new record in the database Track *track2 = [NSEntityDescription insertNewObjectForEntityForName: @"Track" inManagedObjectContext: managedObjectContext]; [managedObjectContext save: &amp;error]; tracks = [self fetchTracks]; // Insert a new item in the table's data source [tracks insertObject: track2 atIndex: 0]; // Insert a new row in the table NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0]; addTrack.track = [tracks objectAtIndex: indexPath.row]; NSLog(@"here"); [[NSNotificationCenter defaultCenter] postNotificationName:@"choosePressed" object:self]; } </code></pre> <p>After add is called it sends a message to another view controller where the user picks a song from mpmediapicker and then hits a save button which selects this method</p> <pre><code>- (void) finishedEditing { if(start.text.length == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil]; alert.tag = 1; [alert show]; [alert release]; } else if(duration.text.length == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil]; alert.tag = 1; [alert show]; [alert release]; } else if(name.text.length == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must fill out all fields" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil]; alert.tag = 1; [alert show]; [alert release]; } else if(songLabel.text.length == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"You must select a song" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil]; alert.tag = 1; [alert show]; [alert release]; } else { if (repeat) { track.title = NULL; track.name = NULL; repeat = NO; } else { songLabel.text = @""; artistLabel.text = @""; [self viewWillDisappear: YES]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; track.name = name.text; track.duration = duration.text; track.startloc = start.text; track.title = [song valueForProperty:MPMediaItemPropertyTitle]; NSManagedObjectContext *context = track.managedObjectContext; NSError *error = nil; [context save: &amp;error]; [[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self]; } } } </code></pre> <p>Im using a NSFetchRequest not a fetchedResults controller. Heres the code.</p> <pre><code>request = [[NSFetchRequest alloc] init]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES]; sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; request.sortDescriptors = sortDescriptors; [sortDescriptor release]; error = nil; entity = [NSEntityDescription entityForName: @"Track" inManagedObjectContext: managedObjectContext]; request.entity = entity; </code></pre> <p><strong>UPDATE</strong></p> <p>Alright so I added a line to print the array in the else statement here</p> <pre><code>else { songLabel.text = @""; artistLabel.text = @""; [self viewWillDisappear: YES]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; track.name = name.text; track.duration = duration.text; track.startloc = start.text; track.title = [song valueForProperty:MPMediaItemPropertyTitle]; NSManagedObjectContext *context = track.managedObjectContext; NSError *error = nil; [context save: &amp;error]; NSLog(@"array: %@", tracks); [[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self]; } </code></pre> <p>And this is what it is printing. I don't know what Im missing. Its making me crazy. And I obviously added values to the name, duration and start labels.</p> <pre><code>array: ( "&lt;Track: 0x136d80&gt; (entity: Track; id: 0x13a120 &lt;x-coredata://9D789618-64C1-40BF-9F15-BA6A3EA715D3/Track/p7&gt; ; data: {\n duration = nil;\n name = nil;\n startloc = nil;\n title = nil;\n})" ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload