简体   繁体   中英

iPhone - core data issue when adding

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.

Here is the add method

- (void) addPressed:(NSNotification *) message
{
// Insert a new record in the database
Track *track2 = [NSEntityDescription insertNewObjectForEntityForName: @"Track" inManagedObjectContext: managedObjectContext];

[managedObjectContext save: &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];


}

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

- (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: &error];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self];

    }


}

}

Im using a NSFetchRequest not a fetchedResults controller. Heres the 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;

UPDATE

Alright so I added a line to print the array in the else statement here

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: &error];

        NSLog(@"array: %@", tracks);

        [[NSNotificationCenter defaultCenter] postNotificationName:@"myTraxsTab" object:self];

    }

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.

array: (
"<Track: 0x136d80> (entity: Track; id: 0x13a120 <x-coredata://9D789618-64C1-40BF-9F15-BA6A3EA715D3/Track/p7> ; data: {\n    duration = nil;\n    name = nil;\n    startloc = nil;\n    title = nil;\n})"
)

If you didn't have any crush crash (and not only in this case), first I would replace the string:

[managedObjectContext save: &error];

with something like:

// Save the managed object context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Failed to save object to managed object context: %@, %@", error, [error userInfo]);
}

Perhaps (and I think most likely), NSlog in this case will give you some info. In any case, man, saving is a quite dangerous operation and you shouldn't leave it without additional, at least small, check.

Good luck!

EDIT:
Could you post how actually you make a fetching of the objects from the database. As far as I understand, you perform it here:

tracks = [self fetchTracks];

But what is there?
Also, I find a bit confusing the frase:

Im using a NSFetchRequest not a fetchedResults controller.

You create an object of NSFetchRequest in order to describe , what you want to retrieve (fetch) from the database. You do it properly, but I don't see in the code where you actually use this request then...
For the purpose of retrieving NSFetchedResultsController is being used. You set it up smth. like:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
                                                         initWithFetchRequest:request 
                                                         managedObjectContext:self.managedObjectContext
                                                         sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

And then, to perform the actual fetch, you do smth. like:

if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

(after this you can get all the objects from fetchedResultsController)

OR , if you don't want to use controller, directly fetch to array:

NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
if (array == nil)
{
    // Deal with error...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM