0

I have an app that crashes on startup in the app store review, but works perfectly otherwise on devices and on the simulators. Here are the specifics.

The app is written for IOS 7, and is being tested on IOS 8.x. I symbolicated the crash logs from Apple, and it is crashing on the first attempt to access information stored in the pre-populated Core Data sqlite db.

This code does the db copy at startup:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory]     URLByAppendingPathComponent:@"CC.sqlite"];
    NSArray *paths =     NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *storePath = [documentsDirectory     stringByAppendingPathComponent: @"CC.sqlite"];

    // Check if the sqlite store exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
        NSLog(@"sqlite db not found... copy into place");

        // copy the sqlite files to the store location.
        NSString *bundleStore = [[NSBundle mainBundle]    pathForResource:@"CC" ofType:@"sqlite"];
         [[NSFileManager defaultManager] copyItemAtPath:bundleStore     toPath:storePath error:nil];

    }
    else {
        NSLog(@"Already exists");
    }
    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator     alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary *options = @{ NSSQLitePragmasOption :     @{@"journal_mode" : @"DELETE"} };


    if (![__persistentStoreCoordinator     addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL     options:options error:&error])
    {
                NSLog(@"error %@, %@", error, [error userInfo]);

    }

    return __persistentStoreCoordinator;
}

We have run this a couple of hundred times on various IOS devices (by several different people) without any problems at all, but invariably get a startup crash at Apple.

It is clear to me from the crash logs that the app (during app store review) is trying to access a non-existant sqlite db through Core Data, but I have no idea why this is happening only at Apple, and why I cannot reproduce the error. I am not sure what other info to add to the question but will happily update as required.

Any advice gratefully received....

jww
  • 97,681
  • 90
  • 411
  • 885
jmf1205
  • 437
  • 6
  • 23
  • Could they be doing a test to see how you handle the database not existing? Perhaps this is by design. – James Black Mar 03 '15 at 03:52
  • Possible, but for an app that is totally dependent upon the existence of the db... seems like a moot point. I wouldn't know what to do in the case of such a catastrophic failure in a production environment.... – jmf1205 Mar 03 '15 at 03:57
  • Have you checked the bundle that is being submitted to Apple to confirm that the database is included? – Paulw11 Mar 03 '15 at 05:07
  • To elaborate on the comment by @Paulw11 - I make a build, find it in the derived data folder (through Xcode), then inspect the build. The preload DB is there. I then drag the build to the Xcode Devices window, for the clean iphone in question, drop the build on the phone, and it works correctly – jmf1205 Mar 03 '15 at 06:22
  • @jmf1205 have you been able to find a solution? i feel like I have the exact same problem now: http://stackoverflow.com/questions/30135840/app-rejected-for-crash-on-persistenstore-creation – Sebastian Flückiger May 09 '15 at 09:43

1 Answers1

0

I will answer my own question here, although the code changes I made while trying to solve the problem makes posting the code not very helpful.

I had to put the startup database operations in a completion block. This involved moving everything into my initial startup view controller, and disabling my tab bar buttons while the database was initialized.

What was happening was certain items in the Core Data sqlite database were being required before it was completely preloaded, thus the crash.

More on blocks can be found here

jmf1205
  • 437
  • 6
  • 23