Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CoreData in Xcode 8?

I am trying use CoreData, but when I add it to my project I only get two new methods :

- (NSPersistentContainer *)persistentContainer

and

- (void)saveContext

Now I can't get old methods to work with CoreData, and I can't find any tutorials with these new methods and Objective-C. How can I save and get data from CoreData using persistentContainer in Xcode 8 with Objective-c?

like image 265
scorpio Avatar asked Dec 08 '16 13:12

scorpio


People also ask

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

How do I use Core Data in Xcode?

Open Xcode and create a new iOS project based on the Single View App template. Name the app HitList and make sure Use Core Data is checked. Checking the Use Core Data box will cause Xcode to generate boilerplate code for what's known as an NSPersistentContainer in AppDelegate.

How do I get data from Core Data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.

Should I use Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.


1 Answers

You can Get context as -

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

or as in Objective-C

NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;

And fetch data like -

var resultArray  = try self.context.fetch(EntityName.fetchRequest())

or as in Objective-C

NSFetchRequest<EntityName *> *fetchRequest = [EntityName fetchRequest];
NSError *error ;
NSArray *resultArray= [context executeFetchRequest:fetchRequest error:&error];

And fetch data with sorting -

var resultArray = [EntityName]()
do {
        let request : NSFetchRequest<EntityName> = EntityName.fetchRequest()
        let sortDescriptor = NSSortDescriptor(key: "somekey", ascending: true)
        let sortDescriptors = [sortDescriptor]
        request.sortDescriptors = sortDescriptors
        resultArray = try self.context.fetch(request)
} catch {
        print("Error")
}

or as in Objective-C

NSFetchRequest<EntityName *> *fetchRequest = [EntityName fetchRequest];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"someKey" ascending:YES];
fetchRequest.sortDescriptors = @[sortDescriptor];
NSError *error ;
NSArray *resultArray= [context executeFetchRequest:fetchRequest error:&error];

And add data like -

let entityNameObj = EntityName(context: context)
entityNameObj.title = "title"

or as in Objective-C

NSManagedObject *entityNameObj = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context];
[entityNameObj setValue:@"someValue" forKey:@"someKey"];

And save context like -

do {
     try self.context.save()
} catch _ as NSError {
     print("Error")
}

or as in Objective-C

[((AppDelegate*)[[UIApplication sharedApplication] delegate]) saveContext];
like image 98
Nikhil Manapure Avatar answered Oct 19 '22 23:10

Nikhil Manapure