How to incorporate Core Data into an already created iOS project in Xcode?

I forgot to enable Core Data, and now I have completed half of the project and want to enable it now.

Can i turn on Core Data now? If anyone can tell me how to do this?

+6
source share
3 answers

Xcode 4.3.2 To add a basic data structure.

Select Target-> Summary Pane-> Linked Framework and Libraries.

enter image description here

In the ABOVE image, the CoreData Framework has already been added. U can click the “+” button below it to add ur selection box.

ONCE U CICK ON Button "+" INCREASES BELOW THE PICTURE SCREEN.

enter image description here

To add new files to it, go to File-> New File → iOS tab-> CoreData setion.You can ur selection file

enter image description here

+11
source

Add the CoreData infrastructure to the project, then create the .xdatamodeld file (File-> New-> CoreData-> Data Model). Name it DataModel. Then create a singleton class that will handle all data storage operations:

.h

// // DataAccessLayer.h // // // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface DataAccessLayer : NSObject @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (strong, nonatomic) NSPersistentStoreCoordinator *storeCoordinator; + (DataAccessLayer *)sharedInstance; - (void)saveContext; @end 

.m

 // // DataAccessLayer.m // // // Created by admin on 2/27/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "DataAccessLayer.h" @interface DataAccessLayer () - (NSURL *)applicationDocumentsDirectory; @end @implementation DataAccessLayer @synthesize storeCoordinator; @synthesize managedObjectModel; @synthesize managedObjectContext; + (DataAccessLayer *)sharedInstance { __strong static DataAccessLayer *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[DataAccessLayer alloc] init]; sharedInstance.storeCoordinator = [sharedInstance persistentStoreCoordinator]; sharedInstance.managedObjectContext = [sharedInstance managedObjectContext]; }); return sharedInstance; } #pragma mark - Core Data - (void)saveContext { NSError *error = nil; if (managedObjectContext != nil) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { NSLog(@"error: %@", error.userInfo); /* 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly." delegate:nil cancelButtonTitle:@"Close." otherButtonTitles:nil, nil]; [alert show]; } } } #pragma mark Core Data stack /** Returns the managed object context for the application. If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. */ - (NSManagedObjectContext *)managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } if (storeCoordinator != nil) { self.managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator:storeCoordinator]; } return managedObjectContext; } /** Returns the managed object model for the application. If the model doesn't already exist, it is created from the application model. */ - (NSManagedObjectModel *)managedObjectModel { if (managedObjectModel != nil) { return managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"]; self.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return managedObjectModel; } /** Returns the persistent store coordinator for the application. If the coordinator doesn't already exist, it is created and the application store added to it. */ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (storeCoordinator != nil) { return storeCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"model.sqlite"]; NSError *error = nil; self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. Typical reasons for an error here include: * The persistent store is not accessible; * The schema for the persistent store is incompatible with current managed object model. Check the error message to determine what the actual problem was. If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application resources directory instead of a writeable directory. If you encounter schema incompatibility errors during development, you can reduce their frequency by: * Simply deleting the existing store: [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] * Performing automatic lightweight migration by passing the following dictionary as the options parameter: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly." delegate:nil cancelButtonTitle:@"Close." otherButtonTitles:nil, nil]; [alert show]; } return storeCoordinator; } #pragma mark Application Documents directory /** Returns the URL to the application Documents directory. */ - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } @end 
+9
source

Hp Iterators iOS Coder and Eugene are true!

The master data file (or project) is configured as follows:

  • and include the underlying data structure (and does this as an import statement in the .pch project .pch ).
  • application delegate header (.h) contains properties declaring context , model and coordinator (as above)
  • app delegate .m defines saveContext , managedObjectContext , managedObjectModel , persistentStoreCoordinator , applicationDocumentDirectory functions / methods
  • data model as above
+3
source

Source: https://habr.com/ru/post/913621/


All Articles