0

I have a method which creates a separate thread:

// Create thread
dispatch_queue_t uniqueQueue = dispatch_queue_create("Unique Email Queue", NULL);

// Run block on another thread called downloadQueue
dispatch_async(uniqueQueue, ^{

        // Get the persistance store coordinator
        AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
        self.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator;

        // Setup the managed object context
        NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];

        // Save to core data for redundancy
        User *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];

        coreDataUser.username = [emailStr lowercaseString];
        coreDataUser.email = emailStr;
        coreDataUser.name = nameStr;

        NSError *error;

        if (![managedObjectContext save:&error])
        {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }            
    }

The app always crashes on this line:

User *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];

I am using this tutorial as a reference: http://www.codigator.com/tutorials/ios-core-data-tutorial-with-example/

My AppDelegate.m file contains this:

- (NSManagedObjectModel *)managedObjectModel {
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

    return _managedObjectModel;
}

//3
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil)
    {
        return _persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"HappyPeople.sqlite"]];

    NSError *error = nil;

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
    {
        /*Error for store creation should be handled in here*/
    }

    return _persistentStoreCoordinator;
}

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

What am I missing?

Also how do I get to see what data is in the database?

cdub
  • 24,555
  • 57
  • 174
  • 303
  • 1
    If you get an exception you should give details. – Wain Nov 23 '13 at 08:45
  • you should have one managed object context per thread. – Neil Galiaskarov Nov 23 '13 at 09:11
  • U are mixing properties and local variables. self.managedObjectContext is a call to property and u have local variable named managedObjectContext. Compiler should be generating a warning. Rename the local MOC and use that MOC, without self. Anyway I don't believe that is what crashes your app. But try. And post the error msg. – AntonijoDev Nov 23 '13 at 10:30
  • nope that's not it, edited it and it was just a typo – cdub Nov 23 '13 at 10:42
  • If it's crashing, there's almost certainly an error message of some kind, and that message is a crucial detail. What is that message? – Tom Harrington Nov 24 '13 at 00:06
  • How do I find that message, knew to xcode debugging. I get a sigbabort – cdub Nov 25 '13 at 04:54

0 Answers0