Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2012, 12:59 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 20
Jonathansm is on a distinguished road
Default Core Data Duplicates

I have this code that checks the database to see if there is a duplicate and that works the only problem is that if there is not a duplicate it won't save the new entry it just crashes the app.

Here is the code
Code:
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];


}   
- (IBAction)save:(id)sender
{    
NSError *error = nil;

        if (_name.text !=nil)
        {
            NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name  contains[cd] %@", _name.text];
            [__fetchedResultsController.fetchRequest setPredicate:predicate];
        }
        
        if (![[self fetchedResultsController] performFetch:&error])
        {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);
        }

        if ([__fetchedResultsController.fetchedObjects count] < 1)
        {
            appDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
            NSManagedObjectContext *context = [appDelegate managedObjectContext];
            NSManagedObject *newManagedObject;
            newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:context];
            [newManagedObject setValue: _name.text forKey:@"name"];
            
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Entry Saved!"
            message:@"This Gun Has Been Saved!" delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil];
            [alert show];
            
            if (![__managedObjectContext save:&error])
            {
                NSLog(@"Problem saving: %@", [error localizedDescription]);
                
            }
        }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Duplicate" message:@"That Entry Already Exists" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
            }
}
Here is the console when is crashes.
Quote:
2012-01-25 11:45:41.351 DataBase 72740:fb03] After managedObjectContext: <NSManagedObjectContext: 0x8992ea0>
2012-01-25 11:45:47.272 DataBase[72740:fb03] CoreData: FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:
2012-01-25 11:45:47.273 DataBase[72740:fb03] CoreData: error: fetch request = <NSFetchRequest: 0x6e66e10> (entity: Entry; predicate: (name CONTAINS[cd] "NOW!!!!"); sortDescriptors: ((
"(name, ascending, compare"
)); batch size: 20; type: NSManagedObjectResultType; )
2012-01-25 11:45:47.274 DataBase[72740:fb03] CoreData: error: cached objects were: (
"<NSManagedObject: 0x6ba9fc0> (entity: Data; id: 0x6ba8680 <x-coredata://8CB27725-AD44-411F-AABA-51351AF0E9FB/Guns/p1> ; data: {\n
)
2012-01-25 11:45:47.275 DataBase[72740:fb03] CoreData: error: fetched objects are: (
)
2012-01-25 11:45:47.277 DataBase[72740:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CoreData: FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'
*** First throw call stack:
(0x154b052 0x1affd0a 0x1185e53 0xa1f1 0x154cec9 0x1d25c2 0x40dd54 0x154cec9 0x1d25c2 0x1d255a 0x277b76 0x27803f 0x2772fe 0x1f7a30 0x1f7c56 0x1de384 0x1d1aa9 0x2438fa9 0x151f1c5 0x1484022 0x148290a 0x1481db4 0x1481ccb 0x2437879 0x243793e 0x1cfa9b 0x2aa8 0x2a05 0x1)
terminate called throwing an exceptionkill
Any ideas?

Last edited by Jonathansm; 01-25-2012 at 02:28 PM.
Jonathansm is offline   Reply With Quote
Old 01-25-2012, 01:39 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

core data does not always make a lot of sense, but with the apps I have, I only generate NSManagedObjectContext in the top appDelegate and then pass that context to all view controllers in the app. So I never create a context in my view controllers. Just use the existing one. That seems to work.
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 01-25-2012, 02:35 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 20
Jonathansm is on a distinguished road
Default

Well I got it working I just had to change this

Code:
    __fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
to this
Code:
    __fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Entry"];
Jonathansm is offline   Reply With Quote
Old 01-25-2012, 03:07 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 20
Jonathansm is on a distinguished road
Default

Never mind it starting crashing again with the same error.
Jonathansm is offline   Reply With Quote
Old 01-25-2012, 03:52 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 20
Jonathansm is on a distinguished road
Default

Well I got it working again I think was the problem was is that I had the cache named the same as the one in the master view. Don't know if that really is the problem or I just got lucky but so far its still working.



__fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
Jonathansm is offline   Reply With Quote
Old 01-25-2012, 04:31 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 20
Jonathansm is on a distinguished road
Default New Question

I have created a view in which I can edit a entry. The edit view displays the everything correctly in text boxes if I change any of those and save it, it adds another entry instead of editing it. How do I make it edit the entry instead of adding another one? I am using NSPredicate to get the data info I don't know if makes any difference.
Jonathansm is offline   Reply With Quote
Old 01-25-2012, 08:27 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by Jonathansm View Post
I have created a view in which I can edit a entry. The edit view displays the everything correctly in text boxes if I change any of those and save it, it adds another entry instead of editing it. How do I make it edit the entry instead of adding another one? I am using NSPredicate to get the data info I don't know if makes any difference.
Just don't insert a new object. Fetch your object, modify it, save your context.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 393
11 members and 382 guests
Atatator, condor304, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0