marciokoko
marciokoko

Reputation: 4986

Why is my app crashing on iPhone 4S because of Core Data request execute?

I have an app that works fine on the simulator 6.1, works fine on the iPhone5 and iPad3 on iOS6.1 but when run on iPhone4S it crashes in this method with Exc Bad Access:

-(void)parsePlistIntoCD{
    self.managedObjectContext = [[SDCoreDataController sharedInstance] backgroundManagedObjectContext];
    // 3: Now put the plistDictionary into CD...create get ManagedObjectContext
    NSManagedObjectContext *context = self.managedObjectContext;
    NSError *error;

    //Create Request & set Entity for request
    NSFetchRequest *holidayRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *topicEntityDescription = [NSEntityDescription entityForName:@"Holiday" inManagedObjectContext:context];
    [holidayRequest setEntity:topicEntityDescription];

    //Create new NSManagedObject
    //Holiday *holidayObjectToSeed = nil;
    Holiday *newHoliday = nil;
    //Execute fetch just to make sure?
    NSArray *holidayFetchedArray = [context executeFetchRequest:holidayRequest error:&error];
    **if (error) NSLog(@"Error encountered in executing topic fetch request: %@", error); // if I comment this line out it reaches as far as the next bold line**

    // No holidays in database so we proceed to populate the database
    if ([holidayFetchedArray count] == 0) {
        //Get path to plist file
        NSString *holidaysPath = [[NSBundle mainBundle] pathForResource:@"PreloadedFarsiman" ofType:@"plist"];
        //Put data into an array (with dictionaries in it)
        NSArray *holidayDataArray = [[NSArray alloc] initWithContentsOfFile:holidaysPath];
        **NSLog(@"holidayDataArray is %@", holidayDataArray);**
        //Get number of items in that array
        int numberOfTopics = [holidayDataArray count];
        //Loop thru array items...
        for (int i = 0; i<numberOfTopics; i++) {
            //get each dict at each node
            NSDictionary *holidayDataDictionary = [holidayDataArray objectAtIndex:i];
            //Insert new object
            newHoliday = [NSEntityDescription insertNewObjectForEntityForName:@"Holiday" inManagedObjectContext:context];
            //Parse all keys in each dict object
            [newHoliday setValuesForKeysWithDictionary:holidayDataDictionary];
            //Save and or log error
            [context save:&error];
            if (error) NSLog(@"Error encountered in saving topic entity, %d, %@, Hint: check that the structure of the pList matches Core Data: %@",i, newHoliday, error);
        };
    }

    //set bool that specifies the coredata has been populated from plist already
    NSString *bundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
    NSString *appFirstStartOfVersionKey = [NSString stringWithFormat:@"first_start_%@", bundleVersion];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:@(YES) forKey:appFirstStartOfVersionKey];
    [prefs synchronize];
}

Why only on 4S? It gives no console log and the last know method traversed in this one above. Here is a pic:

enter image description here

And as mentioned above, if I uncomment that NSLog line it reaches as far as logging the array, as shown in the pic. If I leave it in it stops at that line.

Upvotes: 0

Views: 427

Answers (2)

XJones
XJones

Reputation: 21967

This is a stab in the dark b/c you haven't identified the line of code where the crash occurs. I recommended you set a breakpoint and then step through until you hit the crash. If what I say below doesn't resolve the issue then edit your post and add more info about where the crash occurs and we'll go from there.

Here's something to check. The line:

[newHoliday setValuesForKeysWithDictionary:holidayDataDictionary]

may be crashing if any of the keys in holidayDataDictionary aren't valid for an instance of the Holiday class.

With regard to your question about why it only crashes on your iPhone 4S there's not enough data to explain that yet. Depending on how you've been doing testing and core data migration (if at all) you may have a model inconsistency on that device, though I don't see anything in the screen shot you posted pointing me in that direction. I would try uninstalling/reinstalling your app and see if the crash is still iPhone 4S only.

Upvotes: 0

matt
matt

Reputation: 535925

NSArray *holidayFetchedArray = [context executeFetchRequest:holidayRequest error:&error];
if (error) 
   NSLog(@"Error encountered in executing topic fetch request: %@", error);

No no no no no. I know this is a difficult pattern, but please let's try to do it right. Not if (error). error could be anything (esp. under non-ARC). The test is if (!holidayFetchedArray).

For all of these methods that return a value and also take an NSError** by indirection, you test the result to see if it is nil. If it is, then there was an error because returning nil is the sign that there was an error. Then and only then you may touch the error meaningfully.

The docs are always quite clear about this, though it is true that a fog can come over one's eyes at the critical instance, so I've added some comments in italic brackets to call out the key points:

request

A fetch request that specifies the search criteria for the fetch.

error

If there is a problem executing the fetch, upon return contains an instance of NSError that describes the problem. [And if there is no problem executing the fetch, contains garbage so don't touch it!]

Return Value

An array of objects that meet the criteria specified by request fetched from the receiver and from the persistent stores associated with the receiver’s persistent store coordinator. If an error occurs, returns nil. [And that, forsooth, is the sign that an error did occur.] If no objects match the criteria specified by request, returns an empty array.

This might be the cause of your trouble or it might not, but you must fix it now. I want you to go through all your code looking for NSError* variables declared for use in this pattern and fix all of them! Thank you. Here endeth the lesson.

Upvotes: 2

Related Questions