V
V
Vitaly2016-12-08 15:30:00
Objective-C
Vitaly, 2016-12-08 15:30:00

How to work with Core Data in Objective-C on Xcode 8 and IOS 10, when now only persistentContainer is left here?

All the best! I'm learning programming for ios, I got to core data, but there's a problem - the algorithm or process (I won't say for sure) of working with Core Data has changed. The NSPersistentStore , NSPersistentStoreCoordinator , and NSManagedObjectContext methods are gone :( now only :

#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"HW_02"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving supp



- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

Help a newbie , otherwise I don’t find
tutorials with this :(
- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;

}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
gromwel, 2017-02-24
@gromwel

So far, I only understood that in the line
id delegate = [[UIApplication sharedApplication] delegate];
there is a link to @property sharedApplication, and not to a class method, I still don’t understand how to change

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question