-1

In IOS 10, creating an NSManagedObjectContext and nsmanagedObject was in the followoing:

 NSManagedObjectContext *context =   ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;
    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"Leads"                                  inManagedObjectContext:context];

but, in ios 9 and above, there isnt the presistentContainer, so how do i create an NSManagedObjectContext in IOS 9? I tried the following, but didnt work, it returned nil:

- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}
MrJ
  • 96
  • 10
  • https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS9bookExamples/bk2ch23p824peopleGroupsCoreData/ch36p1079peopleGroupsCoreData/AppDelegate.swift – matt Aug 17 '18 at 11:50

1 Answers1

0

In iOS 9, the instantiation of the NSManagedObjectContext changed to specify the concurrency type for this object.

This means that we now have to make a choice about what thread our managed object context is initialised on: the main queue, or perhaps a special background queue we’ve created. Our choices are:

  • NSPrivateQueueConcurrencyType
  • NSMainQueueConcurrencyType

So the below:

_managedObjectContext = [[NSManagedObjectContext alloc] init];

Should become:

_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

ref: https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext#//apple_ref/c/tdef/NSManagedObjectContextConcurrencyType

TechSeeko
  • 1,521
  • 10
  • 19