Resolved Checking for NSError improperly?

Discussion in 'iOS Programming' started by ArtOfWarfare, Nov 8, 2011.

  1. ArtOfWarfare, Nov 8, 2011
    Last edited: Nov 8, 2011

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    2X Edit: I've narrowed down the issue as having nothing to do with Core Data at all. Instead, the issue appears to be my handling NSErrors improperly. If I remove my checks for errors, it works. If I include the checks for errors, it fails. So, any suggestions as to what I'm doing wrong?

    Okay, so I'm trying to write a really simple Core Data application. Here's what it should do:

    Look to see if Core Data has already been set up. If it has been, display an NSLog showing some of the contents. If it hasn't been, add some contents and save it.

    Initially, my code looked like this (inside of my AppDelegate.m.)
    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [self managedObjectContext];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Key" inManagedObjectContext:__managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        request.entity = entity;
        
        NSError *error;
        Key *foundKey = [[__managedObjectContext executeFetchRequest:request error:&error] lastObject];
        
        if (!error && !foundKey)
        {
            NSLog(@"The fetch returned nothing.");
            Key *key = (Key *)[NSEntityDescription insertNewObjectForEntityForName:@"Key" inManagedObjectContext:__managedObjectContext];
            key.character = @"��";
            NSError *error;
            if (![__managedObjectContext save:&error])
            {
                NSLog(@"Save failed.");
            }
            else
            {
                NSLog(@"Added and saved successfully: %@", key.character);
            }
        }
        
        else
        {
            NSLog(@"The fetch returned this: %@", foundKey.character);
        }
        
        NSLog(@"Log displays this: ��");
        
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    And it would output this:
    Code:
    [b]2011-11-08 11:15:34.835 QuickClip[5150:15803] The fetch returned this: (null)
    2011-11-08 11:15:34.836 QuickClip[5150:15803] Log displays this: ��[/b]
    The line "Log displays this:" was added just to verify that NSLog was capable of displaying emoji characters (it is.)

    Edit: It appears the MacRumors forums are not capable of displaying the emoji. �� Is the emoji character.

    So I decided something was wrong with the fetch. I added in an else if just to check that error was empty. And I found that there actually was an error getting returned. So I tried to get the localized description. Here's what my code looks like at this point:
    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [self managedObjectContext];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Key" inManagedObjectContext:__managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        request.entity = entity;
        
        NSError *error;
        Key *foundKey = [[__managedObjectContext executeFetchRequest:request error:&error] lastObject];
        
        if (!error && !foundKey)
        {
            NSLog(@"The fetch returned nothing.");
            Key *key = (Key *)[NSEntityDescription insertNewObjectForEntityForName:@"Key" inManagedObjectContext:__managedObjectContext];
            key.character = @"��";
            NSError *error;
            if (![__managedObjectContext save:&error])
            {
                NSLog(@"Save failed.");
            }
            else
            {
                NSLog(@"Added and saved successfully: %@", key.character);
            }
        }
        
        else if (!error)
        {
            NSLog(@"The fetch returned this: %@", foundKey.character);
        }
        
        else
        {
            NSLog(@"There was an error with the fetch.");
            NSLog(@"The error is: %@", [error localizedDescription]);
        }
        
        NSLog(@"Log displays this: ��");
        
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    And... this causes a crash and this output:
    Code:
    [b]2011-11-08 11:09:47.607 QuickClip[5116:15803] There was an error with the fetch.
    2011-11-08 11:09:47.609 QuickClip[5116:15803] +[_UIAppearance localizedDescription]: unrecognized selector sent to class 0x75b568
    2011-11-08 11:09:47.610 QuickClip[5116:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[_UIAppearance localizedDescription]: unrecognized selector sent to class 0x75b568'
    *** First throw call stack:
    (0x1e6f052 0x2000d0a 0x1e70add 0x1dd5f00 0x1dd5ce2 0x2f38 0xb89d6 0xb98a6 0xc8743 0xc91f8 0xbcaa9 0x1d59fa9 0x1e431c5 0x1da8022 0x1da690a 0x1da5db4 0x1da5ccb 0xb92a7 0xbaa9b 0x2c5d 0x2bd5)[/b]
    So... for some reason localized description is getting called on _UIAppearance instead of error... that sounds like a retain problem to me... except I'm not getting notified of zombies or anything (and I do had zombie checking turned on for this.)

    Suggestions?

    Edit: Oh, and regarding the first time the code was ran, the time when it should have set up the array, I don't remember, and I didn't think to copy it down, but I believe it told me the save was successful. At the time, I didn't have it outputting what exactly it had saved. Is there any way I can force the app database to start over? I've tried deleting my app from the simulator and doing a clean build, but neither seemed to make it start over with everything.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Try setting your NSError variable to nil when creating it. As in:
    Code:
    NSError *error = nil;
     
  3. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    Supposedly, ARC will now do this for you automatically.

    This is probably not related, but it is a good idea not to use any non-ASCII characters in your source code. If you need to use a string containing non-ASCII characters, write that string out to a UTF8 file, include the file in your bundle, and import the string from that.
     
  4. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Thank you!

    I had completely overlooked that in the tutorial I was copying from.

    I've been working on this project for a few months, since before iOS 5 and ARC was released.

    No, that doesn't seem to be causing any issues. I was worried it would, so all the rest of the database entries stick to ASCII characters.
     

Share This Page